PHP String to Float

433,111

Solution 1

$rootbeer = (float) $InvoicedUnits;

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

Solution 2

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

Solution 3

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));

Solution 4

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.

Solution 5

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;
Share:
433,111
Admin
Author by

Admin

Updated on June 10, 2020

Comments

  • Admin
    Admin almost 4 years

    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 setting these to values:

    $InvoicedUnits = ((string) $InvoiceLineItem->InvoicedUnits);
    $pricePerUnit = ((string) $InvoiceLineItem->PricePerUnit);
    

    If I output this, I get the correct values. Lets say 5000 invoiced units and 1.00 for price.

    Now, I need to show the total amount spent. When I multiply these two together it doesn't work (as expected, these are strings).

    But I have no clue how to parse/cast/convert variables in PHP.

    What should I do?

  • Talvi Watia
    Talvi Watia almost 14 years
    Its the most simple method.. but occasionally I have run into issues doing this with certain database configs and VARCHAR>11 in length.. haven't narrowed down exactly why yet.
  • ontananza
    ontananza almost 12 years
    Instead of ereg use preg $string=preg_replace("/[^0-9\.\-]/","",$string);
  • HADI
    HADI almost 12 years
    Yes, ereg, This function has been DEPRECATED. So need to use preg instead ereg :)
  • zamnuts
    zamnuts over 10 years
    Nowadays (+3 years), one could use NumberFormatter as of PHP 5.3 with the intl extension.
  • Tobias P.
    Tobias P. over 10 years
    PHP 5.3 had been available when i answered the question, but until your comment i was not aware of this single function as there are dozens of libraries doing something similar. But they won't have such a good coverage of locales as the intl extension has. Thanks, i'll try to remember it.
  • Dr. Gianluigi Zane Zanettini
    Dr. Gianluigi Zane Zanettini over 8 years
    kudos for mentioning the locale-awareness. I just spent 1,5h tracking down an issue caused by different locales causing (float) to convert on the first server to "," and on the second to ","
  • Admin
    Admin over 7 years
    ^worth noting that (float) removes 0 from string if it's the first digit
  • Hafenkranich
    Hafenkranich about 7 years
    any comments on why this is bad / where does it fail?
  • Hafenkranich
    Hafenkranich almost 7 years
    or is there a likewise flexible way which is faster?
  • Leif
    Leif over 6 years
    It uses the e flag, which is "no longer supported". That's my guess.
  • CodeJunkie
    CodeJunkie over 4 years
    Also note that "11,000.00" will output 11 instead of the expected 11000.00
  • Uriahs Victor
    Uriahs Victor over 3 years
    Adding the output that would be received in each of those cases as a comment would help a ton without us having to test out each scenario
  • Mujahid Bhoraniya
    Mujahid Bhoraniya about 3 years
    @HADI Nice working for me. very appriciate dear
  • mickmackusa
    mickmackusa over 2 years
    A . inside of a character class does not need to be escaped.