检查字符串是否是Swift中的有效double值

时间:2015-05-19 02:23:09

标签: string swift double

在Swift中,如何检查字符串是否为有效的double值?我一直在使用this question中的以下扩展名(但作为一个浮点数),但如果无法转换该值,则只返回" 0":

nil

理想情况下,我希望它返回if-let,以便它可以被if let i = str.doubleValue { object.price = i } else { // Tell user the value is invalid } 捕获,就像这样:

<?php
class Point2D {

    public $x;
    public $y;

    function __construct($x, $y) {
        $this->x = $x;
        $this->y = $y;
    }

    function x() {
        return $this->x;
    }

    function y() {
        return $this->y;
    }

}

class Point {

    protected $vertices;

    function __construct($vertices) {

        $this->vertices = $vertices;
    }

    //Determines if the specified point is within the polygon. 
    function pointInPolygon($point) {
        /* @var $point Point2D */
    $poly_vertices = $this->vertices;
    $num_of_vertices = count($poly_vertices);

    $edge_error = 1.192092896e-07;
    $r = false;

    for ($i = 0, $j = $num_of_vertices - 1; $i < $num_of_vertices; $j = $i++) {
        /* @var $current_vertex_i Point2D */
        /* @var $current_vertex_j Point2D */
        $current_vertex_i = $poly_vertices[$i];
        $current_vertex_j = $poly_vertices[$j];

        if (abs($current_vertex_i->y - $current_vertex_j->y) <= $edge_error && abs($current_vertex_j->y - $point->y) <= $edge_error && ($current_vertex_i->x >= $point->x) != ($current_vertex_j->x >= $point->x)) {
            return true;
        }

        if ($current_vertex_i->y > $point->y != $current_vertex_j->y > $point->y) {
            $c = ($current_vertex_j->x - $current_vertex_i->x) * ($point->y - $current_vertex_i->y) / ($current_vertex_j->y - $current_vertex_i->y) + $current_vertex_i->x;

            if (abs($point->x - $c) <= $edge_error) {
                return true;
            }

            if ($point->x < $c) {
                $r = !$r;
            }
        }
    }

    return $r;
}

4 个答案:

答案 0 :(得分:23)

编辑/更新: Xcode 9或更高版本•Swift 4或更高版本

您可以使用Double initialiser StringProtocol创建扩展名,并使用它来检查您的字符串是否为有效的double,如下所示:

extension StringProtocol {
    var double: Double? {
        return Double(self)
    }
    var float: Float? {
        return Float(self)
    }
    var integer: Int? {
        return Int(self)
    }
}

测试

let str = "2.9"
if let value = str.double  {
    print(value)           // "2.9\n"
} else {
    print("invalid input")
}

str.prefix(1).integer  // 2
str.suffix(1).integer  // 9

答案 1 :(得分:12)

每次进行转换时,不创建数字格式化器确实更有效:

extension String {
     struct NumFormatter {
         static let instance = NumberFormatter()
     }

     var doubleValue: Double? {
         return NumFormatter.instance.number(from: self)?.doubleValue
     }

     var integerValue: Int? {
         return NumFormatter.instance.number(from: self)?.intValue
     }
}

答案 2 :(得分:2)

为什么不让它返回true?或者extension String { func isInt() -> Bool { if let intValue = Int(self) { if intValue >= 0 { return true } } return false } func isFloat() -> Bool { if let floatValue = Float(self) { if floatValue >= 0 { return true } } return false } func isDouble() -> Bool { if let doubleValue = Double(self) { if doubleValue >= 0 { return true } } return false } func numberOfCharacters() -> Int { return self.characters.count } } 当然。

#!/usr/bin/env perl6
my $listen = IO::Socket::INET.new(:listen, :localhost<localhost>, :localport(3333));
loop {
    my $conn = $listen.accept;
    while my $buf = $conn.recv(:bin) {
        $conn.write: $buf;
    }
    $conn.close;
}

答案 3 :(得分:-6)

这是我的功能:

func je_numerik(text:Character)->Bool //en znak
{
    if((text=="0")||(text=="1")||(text=="2")||(text=="3")||(text=="4")||(text=="5")||(text=="6")||(text=="7")||(text=="8")||(text=="9")){
        return true
    }
    else
    {
        return false
    }
}

func je_stevilka(text: String)->Bool
{
    var pika:Character
    pika="."

    var je_stevilka=true
    //var znaki = Character(text)
    var znaki=Array(text)
    var stevilo_znakov=znaki.count

    if(stevilo_znakov==0)
    {
        je_stevilka=false
    }
    else
    {
        if(stevilo_znakov==1)
        {
            if(je_numerik(znaki[0]))
            {
                je_stevilka=true
            }
            else
            {
                je_stevilka=false
            }
        }
        else
        {

            if((je_numerik(znaki[0])) && (!((znaki[0]=="0")&&((znaki[1]=="0"))))&&(!((znaki[0]=="0")&&((je_numerik(znaki[1]))))))
            {

                var je_pika=false
                for var i = 0; i < stevilo_znakov; i++
                {
                    if(je_numerik(znaki[i])||(znaki[i]==pika))
                    {
                        if(znaki[i]==pika)
                        {
                            if(je_pika)
                            {
                                je_stevilka=false
                            }
                            else
                            {
                                je_pika=true
                                if(stevilo_znakov==(i+1))
                                {
                                    je_stevilka=false
                                }
                            }
                        }
                    }
                    else
                    {
                        je_stevilka=false
                    }
                }
            }
            else
            {
                je_stevilka=false
            }
        }
    }

    return je_stevilka
}

就这样称呼它:

var check_if_is_number=je_stevilka(numberText)