473,289 Members | 1,866 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,289 software developers and data experts.

Math.round question

Using the number 1.015 [many numbers having a 5 at the third decimal point
could be used but not all]

*****************************
the following script returns 1.02, which is correct

var T;
var S=new String(Math.round(parseFloat(1.015).toFixed(2)*100 ));
while (S.length<2) S='0'+S
var temp2 = S.substr(0, T=(S.length-2)) + '.' + S.substr(T, 2);
temp2 = 1.02

but it causes .005 - .009 to return .00, which is wrong
*****************************

******************************
the following script returns 1.01, which is wrong

var T;
var S=new String(Math.round(parseFloat(1.015)*100));
while (S.length<2) S='0'+S
var temp2 = S.substr(0, T=(S.length-2)) + '.' + S.substr(T, 2);
temp2 = 1.01

but it causes .001 - .004 to return .00, which is correct
and .005 - .009 to return .01, which is also correct
******************************
Do you have any ideas as to why it is this way, and if there is a better
[more exact] way to round numbers, these methods seem a little inconsistent.
I'll use method 1 because anything past .009 rounds correctly but what's up
with .005 - .009 rounding to .00 and not .01.
Method 2 just has way to many numbers that round incorrectly.

--

May 18 '06 #1
1 1535
> Using the number 1.015 [many numbers having a 5 at the third decimal point
could be used but not all]

*****************************
the following script returns 1.02, which is correct

var T;
var S=new String(Math.round(parseFloat(1.015).toFixed(2)*100 ));
while (S.length<2) S='0'+S
var temp2 = S.substr(0, T=(S.length-2)) + '.' + S.substr(T, 2);
temp2 = 1.02

but it causes .005 - .009 to return .00, which is wrong
*****************************

******************************
the following script returns 1.01, which is wrong

var T;
var S=new String(Math.round(parseFloat(1.015)*100));
while (S.length<2) S='0'+S
var temp2 = S.substr(0, T=(S.length-2)) + '.' + S.substr(T, 2);
temp2 = 1.01

but it causes .001 - .004 to return .00, which is correct
and .005 - .009 to return .01, which is also correct
******************************

Do you have any ideas as to why it is this way, and if there is a better
[more exact] way to round numbers, these methods seem a little inconsistent.
I'll use method 1 because anything past .009 rounds correctly but what's up
with .005 - .009 rounding to .00 and not .01.
Method 2 just has way to many numbers that round incorrectly.


Two guesses:

1) "bankers rounding" is used, where a ".5" is rounded to the nearest
EVEN integer
2) 1.015 isn't 1.015 but 1.01499 (or something like that)

Is this jscript.net? You might want to ask in a jscript.net specific
group. You question has more to do with the exact workings of the
language than with asp.net.

Hans Kesting
May 19 '06 #2

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

Similar topics

6
by: ng_mr | last post by:
No, not a question about "banker's rounding" or whatever it's called. I want to round a double to the nearest 100th, so I perform the following: // original is a double double result =...
12
by: Woody Splawn | last post by:
I am trying to determine the age of a person based on two dates, the Date of Birth and Today(). I have a function that does this but it is kludgey and is giving me an age that is pretty close...
10
by: tmeister | last post by:
I'm trying to determine the best approach for rounding in an application I'm building. Unfortunately it appears as though SQL Server and VB.NET round in different ways. SQL Server select...
7
by: bravesplace | last post by:
Hello, I am using the folling funtion to round a number to a single digit on my form: function round1(num) { return Math.round(num*1)/1 }
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
8
by: Gonza | last post by:
Hi group, i have a method in my class with the following code: private double GetCompletedPercentage(TimeSpan totalTime, TimeSpan budgetedTime) { double totalMinutes = totalTime.TotalMinutes;...
6
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
9
by: DaiOz | last post by:
Hi guys im doing a course which includes some JavaScript and I need help with the following question please, The code I have so far is below but I don't think its right please help as I need...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.