Problems with number_format(PHP) by two points in the numeral

Asked

Viewed 71 times

2

I’m having trouble with the job number_format PHP, because my numeral comes from the database with two dots, eg:

1.235.32

I’m using the number_format thus:

number_format(floatval('1.235.32'), 2, ',', '.');

And he gives it to me:

1.22

I need my result to be formatted like this:

1235,32

  • Jeez, comes with two points? how is your bank the data type?

  • the formatting must be some standard, this format 1.235.32 is not a valid format, if you have no way to fix the input in the database, you will have to replace the point to work the formatting.

  • @Virgilio.Novic , Eduardo Really, I was formatting wrong before sending to the database. They think I should delete the question?

  • @Talesbreno perhaps it is better to post now a response from you telling where you were wrong and how you solved the problem.

2 answers

1


If data input cannot be changed, use this solution:

// Elimina a pontuação
$value = str_replace('.', '', '100.235.32');

// Adiciona a pontuação correta
$value = substr_replace($value, '.', strlen($value) - 2, 0);

echo number_format($value, 2, ',', '.');

substr_replace - Replaces text within a part of a string

0

Tell you what, that would be it?:

 substr(str_replace('.','','1.235.32'),0,strlen(str_replace('.','','1.235.32'))-2).".".substr('1.235.32',-2);

Half a gabiarra but I think that’s it... I hope I’ve helped!!

Browser other questions tagged

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