5 Replies - 511 Views - Last Post: 20 August 2012 - 12:59 AM Rate Topic: -----

#1 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 171
  • Joined: 25-July 07

How to round downward a decimal number keeping 2 decimal digits?

Posted 18 August 2012 - 03:38 PM

I need to round downward some decimal numbers, but I have to keep the 2 decimal digits, so I can't use floor()

Let's say the number is 0.555555, I need to get 0.55
echo floor(0.555555); //prints 0
echo round(0.555555,2); //prints 0.56
echo number_format(0.555555,2); //prints 0.56



How can I get 0.55 from 0.555555?
Of course this is an example, I could also receive 1.888 (and I need 1.88 from it) or 23.7777777777777777 (and I need to get 23.77)

This post has been edited by Alhazred: 18 August 2012 - 03:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to round downward a decimal number keeping 2 decimal digits?

#2 laytonsdad  Icon User is online

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 380
  • Joined: 30-April 10

Re: How to round downward a decimal number keeping 2 decimal digits?

Posted 18 August 2012 - 04:11 PM

All of your examples are rounding, what you are asking is not.

Why is it important to keep the same numbers (.88 instead of .89)?

Are you doing math with the outcome?

Have you tried to explode the number and takeout what you need?
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: How to round downward a decimal number keeping 2 decimal digits?

Posted 18 August 2012 - 04:25 PM

if you want to truncate you could multiply the number by 10 to the number of digits desired, force type to an integer, and then divide by 10 to the number of digits desired.

Other options include number_format() and sprintf().
Was This Post Helpful? 2
  • +
  • -

#4 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 171
  • Joined: 25-July 07

Re: How to round downward a decimal number keeping 2 decimal digits?

Posted 18 August 2012 - 04:39 PM

View PostCTphpnwb, on 18 August 2012 - 05:25 PM, said:

if you want to truncate you could multiply the number by 10 to the number of digits desired, force type to an integer, and then divide by 10 to the number of digits desired.

Other options include number_format() and sprintf().

Thanks, I'd solved in this way
echo (int) ($decimal_number * pow(10, 2)) / pow(10, 2);


Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: How to round downward a decimal number keeping 2 decimal digits?

Posted 18 August 2012 - 04:42 PM

pow() is computationally expensive, so it's better to use it once and store the value:
$x = 45.5555555;
for($digits = 1; $digits < 5; $digits++) {
	$factor = pow(10,$digits);
	$y = (int)($x * $factor)/$factor;
	echo $x." => ".$y."<br>";
}

Was This Post Helpful? 2
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2899
  • View blog
  • Posts: 7,555
  • Joined: 08-June 10

Re: How to round downward a decimal number keeping 2 decimal digits?

Posted 20 August 2012 - 12:59 AM

why not converting the number to a string and then cut the string appropriately?
// untested
echo substr((string) $number, 0, strpos(".")+3);

This post has been edited by Dormilich: 20 August 2012 - 01:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1