Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-22-2012, 08:13 PM   PM User | #1
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
KB MB division decimal point

Hi

Trying to do some simple division with KB
to get something like 0.5 megabyte

PHP Code:
$sum round(500.30 10242);
echo 
$sum
best round gives me is 0.49

Thanks
Sonny
sonny is offline   Reply With Quote
Old 10-22-2012, 08:46 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Which is correct though. The numbers you have are ~0.48857, which rounded to two decimal points is 0.49. If you want 0.5, you round to 1 decimal point, but that means ever calculation will round to the nearest 1/10th instead of 1/100th.
Fou-Lu is offline   Reply With Quote
Old 10-22-2012, 08:49 PM   PM User | #3
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
http://php.net/manual/en/function.round.php
set number of decimal places to 1 and you'll get 0.5
patryk is offline   Reply With Quote
Old 10-22-2012, 08:53 PM   PM User | #4
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
Fou-Lu
How do you display lets say 0.7 mb?
I messed with floor and others but could
not get it.

If I set round to 1 decimal, it will just just
round up to one mb. floor will round down
to 0 mb

Sonny
sonny is offline   Reply With Quote
Old 10-22-2012, 09:47 PM   PM User | #5
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
round(0.732131312, 1) will return 0.7
that's how it works. also there are flags you can set as third argument if you want other type of rounding.
check link i've posted above for details
patryk is offline   Reply With Quote
Old 10-23-2012, 12:15 AM   PM User | #6
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
Quote:
Originally Posted by patryk View Post
round(0.732131312, 1) will return 0.7
that's how it works. also there are flags you can set as third argument if you want other type of rounding.
check link i've posted above for details
Hi

I know that, but from 973.0 up it will show 1MB,
problem is 1MB = 1024, so I am trying to
round down to 0.9 in this case.

I guess I could do some IF between 973 -1024
and use 2 decimals. just thought I could do that
with some rounding function directly.

Thanks
Sonny
sonny is offline   Reply With Quote
Old 10-23-2012, 12:43 AM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You have to choose between using a printformatter, rounding, or casting. You cannot have the best of both worlds unless you want to start handling on steps of 512 instead of 1024.
Using a printf will truncate off the remaining digits. But that means your 500.30 would result in 0.49. Rounding will make 500.30 into 0.5, but will convert 0.99 to 1.0. You need to choose.
Or of course you can simply write something that shows < 1024KB as a KB rating, and handle it from there. Then you'd see 500.30KB, and at 1033KB you'd see 1.01MB. Easiest done if you measure in a non-multiple unit (bytes). Writing a function like this would be trivial.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:05 AM.


Advertisement
Log in to turn off these ads.