Problems with number format - Adding characters [closed]

-3

I want to return a value of a variable, but when I enter the number_format function in the variable, it ends up adding 2 zeros at the end of the value

$valor = 5300000; // Aqui o número só está sem a formatação | aqui só adicionaria a virgula e o ponto e ficaria: 53,000.00
$valor_f = number_format($valor, 2, ",", ".");

It is returning me 5,300,000.00, I would like it to return 53,000.00.

    
asked by anonymous 13.12.2018 / 11:57

2 answers

3

If you need to change the number "five million and three hundred thousand" into "fifty-three thousand" obviously that will not be just formatting, because they are different values.

In this very specific case you just divide by 100:

$valor_f = number_format($valor/100, 2, ",", ".");  // 53,000.00

But I do not guarantee that this will work for all the cases you need, so it's rather unusual to want to get another value from the formatting.

    
13.12.2018 / 12:07
1

Let's take steps. 1st convert an integer to float:

$valor = sprintf("%.2f", 5300000);

2nd place number_format:

$valor_f = number_format($valor, 2, ",", ".");

Note that in this way, you can replace the 5300000 with any other number.

    
13.12.2018 / 12:06