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

Page 1 of 1

## 5 Replies - 511 Views - Last Post: 20 August 2012 - 12:59 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=289297&amp;s=1fbfe6bcee527901b2b7a9cd518fcaaa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Alhazred

Reputation: 9
• 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?

• D.I.C Regular

Reputation: 62
• 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?

### #3 CTphpnwb

• D.I.C Lover

Reputation: 2486
• 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().

### #4 Alhazred

Reputation: 9
• 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

CTphpnwb, 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);

```

### #5 CTphpnwb

• D.I.C Lover

Reputation: 2486
• 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>";
}
```

### #6 Dormilich

• 痛覚残留

Reputation: 2899
• 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