Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #3
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    http://php.net/manual/en/function.round.php
    set number of decimal places to 1 and you'll get 0.5

  • #4
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    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

  • #6
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    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

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •