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.
-- 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 =...
|
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...
|
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...
|
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
}
|
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)...
|
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;...
|
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'...
|
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 !!!!
|
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...
|
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...
|
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"....
|
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...
|
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 =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
| |