473,378 Members | 1,368 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,378 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 1540
> 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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.