Configuration php monetary mask

Asked

Viewed 23 times

-2

I am developing an application and I have integer values, and in case I need to convert 95686448 to (0,96) cents but using number_format() I have a 95.686.448,00 return as I correctly configure the number_format() to have the desired return ?

1 answer

1


If the number to be converted always has the same amount of digits, just divide it by the number that represents R $ 1,00 (in your case 100.000.000)

<?= number_format( 95686448/100000000,2,',','.') ?> //retorna 0,96
<?= number_format(  9568644/100000000,2,',','.') ?> //retorna 0,01
<?= number_format(956864488/100000000,2,',','.') ?> //retorna 9,57

If you do not know how many digits the number will have but always get tens of cents, just add the string '0. ' at the beginning

<?= number_format('0.'. '95686448'/100000000,2,',','.') ?> //retorna 0,96
<?= number_format('0.'.  '9568644'/100000000,2,',','.') ?> //retorna 0,96
<?= number_format('0.'.'956864488'/100000000,2,',','.') ?> //retorna 0,96

Browser other questions tagged

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