473,326 Members | 2,061 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,326 software developers and data experts.

Rounding values after zeros

Hey guys,

I have a length converter which converts mm, cm, m, km, ft, yd, in and
mi. If I convert 5mm to miles, I get 0.00000031068559649... miles. I'm
looking to convert all my values to two decimal spots, but if it comes
up zeros, I would like to round to two spots after the last zero. I've
looked through google and haven't found anything I could use yet, so I
was wondering if you guys had any scripts or any leads I could use to
make it.

Thanks in advance,
Josh.

Jul 23 '05 #1
10 1768
"joshb" <jb****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hey guys,

I have a length converter which converts mm, cm, m, km, ft, yd, in and
mi. If I convert 5mm to miles, I get 0.00000031068559649... miles. I'm
looking to convert all my values to two decimal spots, but if it comes
up zeros, I would like to round to two spots after the last zero. I've
looked through google and haven't found anything I could use yet, so I
was wondering if you guys had any scripts or any leads I could use to
make it.


Try adding "significant digits" or "precision" to your searches.

I had a quick look around and I think
yournumber.toPrecision(2)
might do it.

eg: 0.00000031068559649
should become 3.1e-7
Jul 23 '05 #2
Actually I think josh wants the precision to be flexiable depending on
how far the digits go out. I don't see any way to accomplish this
without the use of a RegEx.

mi = "0.00000031068559649";
mi_formatted = mi.match(/.+?\.0+.{2}/); // returns 0.00000031

I don't know how reliable this RegEx for your project, but its should
be a good start.

Jul 23 '05 #3
For the record, yeah, I'm looking to convert something like 0.000043874
into 0.000044.

Thanks for the tip Nathan, I have something to work with now.

Jul 23 '05 #4
JRS: In article <11**********************@g43g2000cwa.googlegroups .com>
, dated Fri, 22 Jul 2005 08:05:28, seen in news:comp.lang.javascript,
joshb <jb****@gmail.com> posted :
I have a length converter which converts mm, cm, m, km, ft, yd, in and
mi. If I convert 5mm to miles, I get 0.00000031068559649... miles. I'm
looking to convert all my values to two decimal spots, but if it comes
up zeros, I would like to round to two spots after the last zero. I've
looked through google and haven't found anything I could use yet, so I
was wondering if you guys had any scripts or any leads I could use to
make it.


In English, that would be rounding to two *significant figures*; if you
search for jargon terms, you'll only find jargoneers.

A search for "Rounding to N Significant Figures" and "Round to N
Significant Figures" should have found at least one of
<URL:http://www.merlyn.demon.co.uk/js-round.htm>
<URL:http://www.merlyn.demon.co.uk/js-demos.htm>
the latter now being better.

You should want a method which converts
0.0000006666 -> 0.00000067
0.0000006987 -> 0.00000070 with trailing zero.

<URL:http://www.merlyn.demon.co.uk/js-demos.htm#TF>, with 5/25.4/36/1760
in the input window, gives /inter alia/ :-
StrSigFigFxd +0.0000031
StrSigFigExp +3.1e-06
ShowSigned +0.00000310685596118667
FixedPoint +0.0000031
where the long number is not quite what you quoted. Since the
difference is not 2 ppm, it's hard to explain.

Read and test the code before using it; it's rather new.

--
© 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 #5
joshb wrote:
Hey guys,

I have a length converter which converts mm, cm, m, km, ft, yd, in and
mi. If I convert 5mm to miles, I get 0.00000031068559649... miles. I'm
looking to convert all my values to two decimal spots, but if it comes
up zeros, I would like to round to two spots after the last zero. I've
looked through google and haven't found anything I could use yet, so I
was wondering if you guys had any scripts or any leads I could use to
make it.

Thanks in advance,
Josh.


Either your conversion factor is wrong (or JavaScript maths functions
are not good enough for what you are after). To convert feet to
metres you multiply by exactly 0.3048.

1 mile = 5,280'
= 1,609.344 m
Using 20 significant digits and MS Excel:

0.005m/1,609.344m = 0.00000310685596118667 (miles)
^^^^^^

JS in Safari gave: 0.0000031068559611866696
Firefox gave: 0.0000031068559611866696
IE (Mac) gave: 0.000003106855961186667
All of which vary from the result you provided above, though not until
after 9 significant digits - which is within your tolerance of 2. I
think 3 is the norm in engineering.


--
Rob
Jul 23 '05 #6
JRS: In article <42e1d4cf$0$21392$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sat, 23 Jul 2005 15:25:35, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.auau> posted :

Using 20 significant digits and MS Excel:

0.005m/1,609.344m = 0.00000310685596118667 (miles)
^^^^^^


I call that 20 decimal places and 15 significant figures.

My Windows 98 calculator gives 3.10685596118666984808717092181659e-6 ;
I sometimes wonder how it does arithmetic, and whether all functions are
as accurate as they purport to be.

The decimal mantissa of the true answer is, I think,
+3.1 068559611866698480871709218165911079296906
where the portion after the space repeats indefinitely.

Javascript specifies 64-bit IEEE Doubles, and standards suggest that
routine Double arithmetic should give the same 64 bits in any system,
unless some calculated value is really near half-way between two 64-bit
numbers. Therefore it is a little disconcerting to see non-identical
decimal results in your browsers and in mine.

I don't recall that the resolution of the conversion is stipulated; but
ISTM that there should be consensus on it.

The OP may be American : maybe the meter is not quite the same as the
metre?

--
© 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 #7


Dr John Stockton wrote:
JRS: In article <42e1d4cf$0$21392$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sat, 23 Jul 2005 15:25:35, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.auau> posted :

Using 20 significant digits and MS Excel:

0.005m/1,609.344m = 0.00000310685596118667 (miles)
^^^^^^


I call that 20 decimal places and 15 significant figures.

My Windows 98 calculator gives 3.10685596118666984808717092181659e-6 ;
I sometimes wonder how it does arithmetic, and whether all functions are
as accurate as they purport to be.

The decimal mantissa of the true answer is, I think,
+3.1 068559611866698480871709218165911079296906
where the portion after the space repeats indefinitely.

Javascript specifies 64-bit IEEE Doubles, and standards suggest that
routine Double arithmetic should give the same 64 bits in any system,
unless some calculated value is really near half-way between two 64-bit
numbers. Therefore it is a little disconcerting to see non-identical
decimal results in your browsers and in mine.

I don't recall that the resolution of the conversion is stipulated; but
ISTM that there should be consensus on it.

The OP may be American : maybe the meter is not quite the same as the
metre?


I wonder why you thought that the meter and metre might be different
other than in spelling. The modernized metric system known as Systeme
International d'Unites is of course the same in the US as in other
countries. The US signed the Meter Convention in Paris in 1875 along
with 16 other countries, and this support remains until present.
Non-metric units, such as miles, are defined in terms of the SI units.
Scientific work and publications are all in modern metric units. Some
engineers were taught in English units. Wine and spirits are now sold
in metric units in the US, but many other food items are still sold by
the pound or US quarts, for example.

The mile has been used in many countries and defined in different ways
for a very long time. Plese see
http://www.absoluteastronomy.com/enc.../m/mi/mile.htm for some of
the history of the mile. One source of confusion can be between the
usual statute(international mile) and the survey mile used in the US,
because the conversion factor from one to the other is 0.999998
exactly. The details are in the reference I quoted.

__________________________________________________ ____

http://www.cwdjr.net/calendar/perpetual_calendar3.html

__________________________________________________ ____

Jul 24 '05 #8
cw******@yahoo.com wrote:
[...]
The OP may be American : maybe the meter is not quite the same as the
metre?
I wonder why you thought that the meter and metre might be different
other than in spelling.


In the words of Foghorn Leghorn: "Heh, heh, that's a joke son".
The modernized metric system known as Systeme
International d'Unites is of course the same in the US as in other
countries. The US signed the Meter Convention in Paris in 1875 along
That would be the Convention du Mètre.

[...] the history of the mile. One source of confusion can be between the
usual statute(international mile) and the survey mile used in the US,
because the conversion factor from one to the other is 0.999998
exactly. The details are in the reference I quoted.


But that does not account for the difference noted. The bottom line
appears to be that JavaScript mathematical functions are implemented
slightly differently in different browsers on different platforms.
--
Rob
Jul 24 '05 #9


RobG wrote:
cw******@yahoo.com wrote:
[...]
the history of the mile. One source of confusion can be between the
usual statute(international mile) and the survey mile used in the US,
because the conversion factor from one to the other is 0.999998
exactly. The details are in the reference I quoted.


But that does not account for the difference noted. The bottom line
appears to be that JavaScript mathematical functions are implemented
slightly differently in different browsers on different platforms.


I did not say that the difference between the US survey mile and the
statute mile had anything to do with JavaScript math functions.

It would in fact be expected that javascript would give somewhat
different math results on different platforms when one considers the
last few significant figures. This is just what happens on large
computers. It is why different constants are given for different
computers in series calculations that wish to push the computer to the
limits. This has been done for many years. To know exactly what is
going on, one needs to know the details of how a computer is making
calculations at a machine level, and this information is not provided
completely by a higher level language such as C++ or Fortran or a lower
level language such as javascript. It is of course possible that some
version of a language, or some routine therein, may have errors in the
code that introduce error in addition to that imposed by the way the
computer works.

__________________________________________________ ___________

http://www.cwdjr.net/geometric/tile_layer4.html

__________________________________________________ ___________

Jul 24 '05 #10


cw******@yahoo.com wrote:
It would in fact be expected that javascript would give somewhat
different math results on different platforms when one considers the
last few significant figures. ---


Go to the test page at http://www.bugnet.com/alerts/bugalert9298.html
for an interesting test for 3 calculations. This page is ancient, being
last updated in 1998. However nothing appears to have changed
concerning math calculation accuracy using javascript since then. I
tested the page on 7 of the most recent computer browsers and the old
NN4. All give the same errors as one got back in 1998. However the
simulator for the old MSNTV 2.8 set top box gave exact values for 2 of
the 3 test cases. However you find it gives only 12 digits after the
decimal point, while the computer browsers all gave 13 places. When you
round off the computer browser results to 12 places, they are in
agreement with the MSNTV results. The reference points out that the
results are exact in Perl calculations. At the time, the browser makers
contacted claimed that the javascript math calculations were doing
exactly what they were supposed to do according to floating point
standards. If you round off properly, you will of course not have this
problem unless you are dealing with extremely large or small numbers in
some special cases. In the old days, most scientific calculations on
the IBM 370 were done in floating point in Fortran, while business
people used the Cobol program that was designed for business
calculations and did not alarm bean counters with things such as
$1.999999999999.

Jul 25 '05 #11

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

Similar topics

22
by: Allen Thompson | last post by:
Is there a script that will round off a number to a certain number of decimal places? -Allen Thompson
43
by: Tim Chase | last post by:
Just as a pedantic exercise to try and understand Python a bit better, I decided to try to make a generator or class that would allow me to unpack an arbitrary number of calculatible values. In...
9
by: Joe Attardi | last post by:
Hi all, Math is not my strongest area so forgive me if I use some of the wrong terminology. It seems that scientific notation is immune to rounding errors. For example: (4.98 * 100) + 5.51 ...
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
18
by: jdrott1 | last post by:
i'm trying to round my currency string to end in 9. it's for a pricing application. this is the function i'm using to get the item in currency: FormatCurrency(BoxCost, , , , TriState.True) if...
5
by: Spoon | last post by:
Hello everyone, I don't understand how the lrint() function works. long lrint(double x); The function returns the nearest long integer to x, consistent with the current rounding mode. It...
248
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) ...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.