PHP String to Float

前端 未结 8 1231
别跟我提以往
别跟我提以往 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:59

    Dealing with markup in floats is a non trivial task. In the English/American notation you format one thousand plus 46*10-2:

    1,000.46
    

    But in Germany you would change comma and point:

    1.000,46
    


    This makes it really hard guessing the right number in multi-language applications.
    I strongly suggest using Zend_Measure of the Zend Framework for this task. This component will parse the string to a float by the users language.

    0 讨论(0)
  • 2020-11-28 06:00

    You want the non-locale-aware floatval function:

    float floatval ( mixed $var ) - Gets the float value of a string.

    Example:

    $string = '122.34343The';
    $float  = floatval($string);
    echo $float; // 122.34343
    
    0 讨论(0)
提交回复
热议问题