-1

I'm having doubts about how to convert an amount of an api I'm using. I have the value of 25000 integers and I need to convert it to $250.00. However, I didn't even find a way to convert these integer values, since normally the amount is a decimal value equal to 250.00 and so on.

What have I tried:

$amount = 25000;
$formatter = new NumberFormatter('en',  NumberFormatter::CURRENCY);
echo $formatter->formatCurrency($amount, 'USD'); //return $25,000.00

How can I get this result:

$amount = 25000; //return $250,00
Giulia
  • 73
  • 1
  • 7
  • 2
    Well for a start you will have to divide it by 100. If you are storing currency as an integer (and thats quite a good idea) of course you have to store the cents/pennies as part of the integer. So before presenting it you will need to divide it by 100 to get the cents/pennies presented – RiggsFolly Oct 04 '21 at 15:25
  • 1
    Give this a look https://www.php.net/manual/en/function.number-format.php – stefan_aus_hannover Oct 04 '21 at 15:26
  • 1
    A library like [Money](https://github.com/moneyphp/money) might be useful if you want to keep the precision of storing the values as integers - the moment you're dividing it by 100 to format it, you'll lose all of that benefit – iainn Oct 04 '21 at 15:28
  • @RiggsFolly Omg hahaha Great, thanks!! – Giulia Oct 04 '21 at 15:29
  • @steveo314 Number_format only accepts float, anyway thanks – Giulia Oct 04 '21 at 15:29
  • @iainn What kind of benefit are you referring to? – Giulia Oct 04 '21 at 15:31
  • @Previell Storing currency values as floats means that they're susceptible to all of the rounding errors and formatting issues as other floating point numbers. [There's a lot more detail in this answer](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency). Or look up "money pattern", and anything referencing Martin Fowler. The library I linked is an implementation of that pattern. – iainn Oct 04 '21 at 15:36
  • @iainn Got it, these values ​​will be stored as integers, I just needed to convert these received values ​​and show them to users, it was more a matter of observation really, RiggsFolly helped me anyway thanks – Giulia Oct 04 '21 at 15:40
  • @Previell did you test it with an integer? – stefan_aus_hannover Oct 04 '21 at 15:48
  • You aren't right; number_format is also working with an integer. I just checked it, try this way `echo '$'.number_format($amount);` – Mainul Hasan Oct 04 '21 at 15:57
  • Incorrect result for `amountFormat(5);` – jspit Oct 22 '22 at 08:56

2 Answers2

1

You can make it with a function, NumberFormat doesn't seems match with what you want to do (as you can read it here).

function amountFormat(int $number, $separator = ",", $currency = "$") {
  $decimal = substr($number, -2);
  $amount = substr($number, 0, -2) . $separator . $decimal;
  return $currency . $amount;
}

And call it

 echo amountFormat(25000); // return $250,00

Hoping it's helped you.

-2

Just follow do very small modification with your code in line 2 and 3.

$amount = 25000;
$formatter = new \NumberFormatter("en-US", \NumberFormatter::CURRENCY);
echo $formatter->format($amount) . "<br>";

I hope it will serve your purpose.

  • 1
    Please add some explanation to your answer such that others can learn from it. What have you changed, and why? – Nico Haase Oct 04 '21 at 16:01