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.rou nd(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.rou nd(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 1553
> 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.rou nd(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.rou nd(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 = Math.Round( original, 2 );
But I'm not happy with the results. E.g. if original is
|
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 but only because I have figured into the equation
(manually) the approximate number of leap years.
The code reads:
Dim TS As New TimeSpan
Dim...
|
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 round(123.465,2)
returns
|
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)
0.74
| |
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;
double budgetedMinutes = budgetedTime.TotalMinutes;
return Math.Round((totalMinutes * 100) / budgetedMinutes,2);
}
|
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' is odd
However, if format.NumberDecimalDigits is 1
decimal d = 3.45M;
d.ToString( "F", format ); //Return 3.5 - this is different from...
|
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 to complete this part to move onto the next.
<HTML>
<HEAD>
<TITLE>M150 TMA 5 : Programming : Task 1 - Testing Math.random()</TITLE>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |