Problems with number format - Adding characters

Asked

Viewed 31 times

-3

I want to return a value of a variable, only when I insert the number_format function into 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, ",", ".");

He’s returning me 5,300,000.00, I’d like you to return 53,000.00.

  • 1

    If you need to convert the number 5 million and 300 to 53 ml it is not just with formatting that you will get. You will need to split by 100 before.

  • 1

    Anyway, if you take the decimal separation character of a number you will get another value. If you need to undo the process, you have to split it. The problem is 3.14159, pi value; if you remove the formatting will be 314159. How will you know how many decimal places the number needs to be?

2 answers

3

If you need to change the number "five and three hundred thousand" in the number "fifty-three thousand" obviously it will not just be formatting, as 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 because it is quite unusual to want to get another value from formatting.

1

We’ll go in stages. 1º convert an integer to float:

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

2º put the number_format:

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

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

  • Now he returns to me: "5,300,000"

  • I tested it was normal for me. I thought the 5300000 were integers, in case you want to take the last two zeros and turn to decimal, use Anderson’s tip.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.