Jump to content

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;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.