about rounding a number

Discussion in 'Javascript' started by Ricardo Garcia, Oct 24, 2004.

  1. hi, i have some file size in bytes, and i want to get it in KB or MB, with
    one decimal digit,
    for example:
    1268777 -> 1.2 MB

    now i can have 1.210000...
    my question is how do you can round this number properly, if i use
    Math.round(number), i only get 1 MB


    Thanks
     
    Ricardo Garcia, Oct 24, 2004
    #1
    1. Advertising

  2. Ricardo Garcia

    Evertjan. Guest

    Ricardo Garcia wrote on 24 okt 2004 in comp.lang.javascript:

    > hi, i have some file size in bytes, and i want to get it in KB or MB,
    > with one decimal digit,
    > for example:
    > 1268777 -> 1.2 MB
    >
    > now i can have 1.210000...
    > my question is how do you can round this number properly, if i use
    > Math.round(number), i only get 1 MB
    >


    n = 1268777 // Bytes
    n = Math.round(n/100000)/10 // 1.3 MegaBytes

    BUT, perhaps you really want:

    n = 1268777 // Bytes
    n = Math.round(n*10/1024/1024)/10 // 1.2 MegaBytes


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)
     
    Evertjan., Oct 24, 2004
    #2
    1. Advertising

  3. JRS: In article <Xns958CE310B3517eejj99@194.109.133.29>, dated Sun, 24
    Oct 2004 20:19:12, seen in news:comp.lang.javascript, Evertjan.
    <> posted :
    >Ricardo Garcia wrote on 24 okt 2004 in comp.lang.javascript:
    >
    >> hi, i have some file size in bytes, and i want to get it in KB or MB,
    >> with one decimal digit,
    >> for example:
    >> 1268777 -> 1.2 MB
    >>
    >> now i can have 1.210000...
    >> my question is how do you can round this number properly, if i use
    >> Math.round(number), i only get 1 MB
    >>

    >
    >n = 1268777 // Bytes
    >n = Math.round(n/100000)/10 // 1.3 MegaBytes
    >
    >BUT, perhaps you really want:
    >
    >n = 1268777 // Bytes
    >n = Math.round(n*10/1024/1024)/10 // 1.2 MegaBytes



    Those round to a multiple of 0.1 (give or take the resolution of an IEEE
    Double); but rounding to a given number of digits is another matter
    entirely.

    The OP's wording calls for 1 MB to be represented as the string 1.0 MB.

    The OP needs to read the newsgroup FAQ (via below), in particular Sec.
    4.6; the first link of that leads to a variety of rounding code,
    originated by several authors.

    However, the example given implies truncation, for which (at least in
    MSIE4) a RegExp might be used :
    x = "000001.98765".match(/.*\.\d/) // 000001.9
    x = 000001.98765 ; x = String(x).match(/.*\.\d/) // 1.9
    though something must be done to ensure a decimal point and sufficient
    digits.
    x = 000001.00 ; x = String(x).match(/.*\.\d/) || x+".0" // 1.0

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Oct 25, 2004
    #3
  4. Ricardo Garcia

    Evertjan. Guest

    Dr John Stockton wrote on 25 okt 2004 in comp.lang.javascript:
    > Those round to a multiple of 0.1 (give or take the resolution of an IEEE
    > Double); but rounding to a given number of digits is another matter
    > entirely.


    I don't quite see that, John.

    Rounding 3 to 4 decimals is simply 3, not 3.0000,
    because rounding is a mathematical concept and does not involve the string
    representation of the result.

    > The OP's wording calls for 1 MB to be represented as the string 1.0 MB.


    I agree, that is another view of the matter on hand entirely.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)
     
    Evertjan., Oct 25, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thor W Hammer

    Formatting a number without rounding

    Thor W Hammer, Nov 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    522
    Karl Seguin
    Nov 22, 2005
  2. iksrazal
    Replies:
    1
    Views:
    742
    Chris Smith
    Jul 3, 2003
  3. Sooha Park Lee

    Rounding a floating point number declared in "double"

    Sooha Park Lee, Jul 16, 2003, in forum: C Programming
    Replies:
    2
    Views:
    494
    Mark McIntyre
    Jul 16, 2003
  4. Sooha Park Lee

    Rounding a floating point number declared in "double"

    Sooha Park Lee, Jul 16, 2003, in forum: C Programming
    Replies:
    2
    Views:
    330
    martinh
    Jul 17, 2003
  5. NoKetch

    Rounding to next highest number?

    NoKetch, Dec 15, 2003, in forum: C Programming
    Replies:
    7
    Views:
    595
    Mark McIntyre
    Dec 15, 2003
Loading...

Share This Page