2

How to convert a string like "3,2563" to "3.2563",

$number = "3,2563" ;
setlocale(LC_MONETARY,"en_US");
echo money_format("%.4n", $number);

or

$number = number_format($number,4,".","");

Both examples output just 3.0000

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Hunt3r
  • 39
  • 6
  • You have to replace the `,` with a `.` first before you can format the number because `3,2563` is _not a number_. – tkausl Feb 03 '16 at 20:22
  • 1
    If you turn on error reporting, you will see that it is telling you there is a number format error and it stopped reading the number at the comma and only read the 3. – kainaw Feb 03 '16 at 20:22
  • Start with numbers that are actually numbers, like `32563` instead of `"3,2563"`. Then format them when outputting them as text. – David Feb 03 '16 at 20:26
  • but my input is from html code from external site. – Hunt3r Feb 03 '16 at 20:28
  • php couldn't care less about national formats. a number in php doesn't use 1000s seperators, or use `,` at all. you started out with a string, and fed that string to a function that expects a number. so your string was converted to a number using php's normal rules, and the comma and everything after it was stripped off. – Marc B Feb 03 '16 at 20:42

3 Answers3

3

The string "3,2563" is not a number, thus - it cannot be used as such. It can easily be converted to a float number, using PHP function str_replace and type casting.

$number = "3,2563";
$number = (float)str_replace(",", ".", $number); // returns (float) 3.2563
// Do whatever you want to do. Now $number is a float.

Using str_replace, the , is replaced with a .

Note that the decimals separator can vary, depending on your PHP configuration.

thexpand
  • 641
  • 13
  • 28
1

"3,2563" is a string, you're trying to display a string as a number, that's not possible.

You can replace , with . before changing its type:

$number = "3,2563";
$number = str_replace(',', '.', $number); // get "3.2563"
$number = (float) $number; // get a floating number
setlocale(LC_MONETARY,"en_US");
echo money_format("%.4n", $number); // shows "3.2563"
echo money_format("%.2n", $number); // shows "3.26"
A.L
  • 10,259
  • 10
  • 67
  • 98
  • 1
    What's the need for lines 3-5 if line 2 has the same result as 5? – scrowler Feb 03 '16 at 20:28
  • @RobbieAverill you're right, the output is the same but that's just a coincidence, if you want to display only 2 cents (2 digits after the dot) you need all these steps. – A.L Feb 03 '16 at 20:31
  • You don't need to specify (float)$number at line 3, the variable type is determined by the context in which the variable is used – SamyQc Feb 03 '16 at 20:32
  • @SamyQc I think it's easier to read this way. – A.L Feb 03 '16 at 20:34
1

You're using a string ill-formatted for the desired use-case and existing logic you have in your code - i.e. '3,2563'. Let me be more clear. In some countries, a comma is used instead of a decimal to demarcate a whole unit of some currency and fractional units of some currency. In other cases, the comma and decimals indicate a thousand whole unit of some currency. It depends on what you're aiming for which isn't clear based on the example you gave... plus, I'm not aware of every monetary syntax convention.

In any event, the general procedure you want to employ is to remove all the commas or to normalize the number (for example use 32563 instead of 3,2563 if you're going for whole units), do your operations, and then reapply the convention (I assume that they're monetary conventions) that you want at the end. If you just want to replace the comma with a decimal - you can still use str_replace() to accomplish that as well. Build a function or class to do that so you can reuse that code for use with other similar problems.

My recommendation, though it wasn't explicit, is to simply use some str_replace() logic to generate a normalized/indexed number.

Adam Gerard
  • 708
  • 2
  • 8
  • 23
  • Can you please improve your answer? It doesn't explain much. Is *using a "non-number" string* a problem? That's not explicit. – A.L Feb 03 '16 at 20:40
  • 1
    Yes, sorry should have been a bit more explicit - added some more verbiage – Adam Gerard Feb 03 '16 at 23:48