473,396 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

about rounding a number

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
Jul 23 '05 #1
3 1240
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)

Jul 23 '05 #2
JRS: In article <Xn********************@194.109.133.29>, dated Sun, 24
Oct 2004 20:19:12, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> 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.
Jul 23 '05 #3
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)

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Raoul Watson | last post by:
By any chance, anyone got a rounding routine that does a work around the VB "round" bug? I still find it amazing that a company as large as Microsoft would put out a math package that is so...
3
by: Dalan | last post by:
Is there any code available to address currency rounding problems in Access 97? Apparently, selecting currency type table fields does not resolve the problem. For instance, in my form I have a...
3
by: Caesar Augustus | last post by:
Hello Tom, Long time listener, first time caller... I have been working with vb code to automate a salary increase process that not only rounds to the nearest penny but also rounds to the...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.