PHP String to Float

前端 未结 8 1230
别跟我提以往
别跟我提以往 2020-11-28 05:37

I am not familiar with PHP at all and had a quick question.

I have 2 variables pricePerUnit and InvoicedUnits. Here\'s the code that is set

相关标签:
8条回答
  • 2020-11-28 05:44

    If you need to handle values that cannot be converted separately, you can use this method:

    try {
        $valueToUse = trim($stringThatMightBeNumeric) + 0;
    } catch (\Throwable $th) {
        // bail here if you need to
    }
    
    0 讨论(0)
  • 2020-11-28 05:46
    $rootbeer = (float) $InvoicedUnits;
    

    Should do it for you. Check out Type-Juggling. You should also read String conversion to Numbers.

    0 讨论(0)
  • 2020-11-28 05:46

    I was running in to a problem with the standard way to do this:

    $string = "one";
    
    $float = (float)$string;
    
    echo $float; : ( Prints 0 )
    

    If there isn't a valid number, the parser shouldn't return a number, it should throw an error. (This is a condition I'm trying to catch in my code, YMMV)

    To fix this I have done the following:

    $string = "one";
    
    $float = is_numeric($string) ? (float)$string : null;
    
    echo $float; : ( Prints nothing )
    

    Then before further processing the conversion, I can check and return an error if there wasn't a valid parse of the string.

    0 讨论(0)
  • 2020-11-28 05:49

    you can follow this link to know more about How to convert a string/number into number/float/decimal in PHP. HERE IS WHAT THIS LINK SAYS...

    Method 1: Using number_format() Function. The number_format() function is used to convert a string into a number. It returns the formatted number on success otherwise it gives E_WARNING on failure.

    $num = "1000.314"; 
    
    //Convert string in number using  
    //number_format(), function 
    echo number_format($num), "\n"; 
    
    //Convert string in number using  
    //number_format(), function 
    echo number_format($num, 2); 
    

    Method 2: Using type casting: Typecasting can directly convert a string into a float, double, or integer primitive type. This is the best way to convert a string into a number without any function.

    // Number in string format 
    $num = "1000.314"; 
    
    // Type cast using int 
    echo (int)$num, "\n"; 
    
    // Type cast using float 
    echo (float)$num, "\n"; 
    
    // Type cast using double 
    echo (double)$num; 
    

    Method 3: Using intval() and floatval() Function. The intval() and floatval() functions can also be used to convert the string into its corresponding integer and float values respectively.

    // Number in string format 
    $num = "1000.314"; 
    
    // intval() function to convert  
    // string into integer 
    echo intval($num), "\n"; 
    
    // floatval() function to convert 
    // string to float 
    echo floatval($num); 
    

    Method 4: By adding 0 or by performing mathematical operations. The string number can also be converted into an integer or float by adding 0 with the string. In PHP, performing mathematical operations, the string is converted to an integer or float implicitly.

    // Number into string format 
    $num = "1000.314"; 
    
    // Performing mathematical operation  
    // to implicitly type conversion 
    echo $num + 0, "\n"; 
    
    // Performing mathematical operation  
    // to implicitly type conversion 
    echo $num + 0.0, "\n"; 
    
    // Performing mathematical operation  
    // to implicitly type conversion 
    echo $num + 0.1;
    
    0 讨论(0)
  • 2020-11-28 05:52

    Use this function to cast a float value from any kind of text style:

    function parseFloat($value) {
        return floatval(preg_replace('#^([-]*[0-9\.,\' ]+?)((\.|,){1}([0-9-]{1,3}))*$#e', "str_replace(array('.', ',', \"'\", ' '), '', '\\1') . '.\\4'", $value));
    }
    

    This solution is not dependant on any locale settings. Thus for user input users can type float values in any way they like. This is really helpful e.g. when you have a project wich is in english only but people all over the world are using it and might not have in mind that the project wants a dot instead of a comma for float values. You could throw javascript in the mix and fetch the browsers default settings but still many people set these values to english but still typing 1,25 instead of 1.25 (especially but not limited to the translation industry, research and IT)

    0 讨论(0)
  • 2020-11-28 05:58

    Well, if user write 1,00,000 then floatvar will show error. So -

    floatval(preg_replace("/[^-0-9\.]/","",$input));
    

    This is much more reliable.

    Usage :

    $input = '1,03,24,23,434,500.6798633 this';
    echo floatval(preg_replace("/[^-0-9\.]/","",$input));
    
    0 讨论(0)
提交回复
热议问题