JRS: In article <
[email protected]>, dated Sun, 24
Oct 2004 20:19:12, seen in Evertjan.
Ricardo Garcia wrote on 24 okt 2004 in comp.lang.javascript:
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