Jump to content

[SOLVED] number_format Question


morlan

Recommended Posts

Hi folks,

 

Im trying to add a decimal point before the last two digits of a numeric string using number_format, but it doesnt seem have the desired effect.

 

I've tried using number_format($number,2), but this adds a decimal point to the end.

 

This is what I'm trying to achieve

1000 = 10.00

10000 = 100.00 and so on.

 

Link to comment
https://forums.phpfreaks.com/topic/64886-solved-number_format-question/
Share on other sites

number_format will only place the decimal point in the specified place if you pass it a float:

$num = '123.456';

echo number_format($num, 2);
// result: 123.46

 

If you pass it a whole number (integer) then it'll add .00 to the end of the number.

 

If you want to always place the decimal before the last two digits of a number you'll have to do it manully:

<?php
$num = '10000';

// get last two digits of number
$l2d = substr($num, -2, 2);
// place decimal before the last two digits of the number
$new_num = substr_replace($num, '.', -2, 2) . $l2d;

echo $num . '<br />' . $new_num;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.