?
Solved

round un number

Posted on 2006-07-06
8
Medium Priority
?
311 Views
Last Modified: 2008-02-01
Hello
here is my code to round un number

/********************************************************/
// Round : round(8.56475, 2) => 8.56
/********************************************************/
function roundFloat( value, n_precision ) {
      var tem = 1.*Math.pow(10, n_precision);
      var retval = Math.round(1.*value*tem)/tem;
      return retval;
}


It works well but I have one problem following

roundFloat( 256.905, 2 )  =  256.9 (false) instead 256.91 (true)

I dit verify and I see the reason : Javascript calculs "1.*256.905*100 = 25690.499999999996" (false) instead 25690.5

I dont know how I can correct it ?
thanks a lot
best regards
Hai

0
Comment
Question by:ndhai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Expert Comment

by:Rakafkaven
ID: 17051017
Javascript treats numbers as doubles, which can result in some bizarre miscalculations that aren't your fault at all.  Silly as it seems, adding a large arbitrary number (say 50000) and then subtracting it again after the operation seems to fix the issue.  Make your number large enough that it won't interfere with any conceivable rounding your function may do.

function roundFloat( value, n_precision ) {
     value = value + 50000;
     var tem = 1.*Math.pow(10, n_precision);
     var retval = (Math.round(1.*value*tem)/tem) - 50000;
     return retval;
}
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 200 total points
ID: 17051459
You waste power :)



     alert( 256.905.toFixed(2)  )


0
 

Author Comment

by:ndhai
ID: 17051561
hello Rakafkaven
thanks but It doestn't work  :((
when I tried alert( roundFloat(256.905, 2) );, it showed 256.910000000149
Hai
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ndhai
ID: 17051591
>> alert( 256.905.toFixed(2)  )

excellent response
thanks a lot
Hai
0
 
LVL 4

Expert Comment

by:Rakafkaven
ID: 17051626
Not only do I waste power, the method I used doesn't work, since it the floating point operations still occur on the item.  Rather than trying to avoid the operations, kicking them into an integer with a second Math.round and an additional decimal shifting operation works consistently.  Of course, it wastes even MORE power, but it does return the answer without the potential for padding that toFixed has.

function roundFloat( value, n_precision ) {
     value = value;
     var tem = 1.*Math.pow(10, n_precision);
     var retval = Math.round(Math.round(value*tem*100)/100)/tem;
     return retval;
}
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17051721
If you want to strip the zeros, then check this:

alert( roundFloat(256.905,2)  )

function roundFloat( value, n_precision ) {
     return value.toFixed(n_precision)*1;
}

0
 
LVL 4

Expert Comment

by:Rakafkaven
ID: 17051774
If I didn't know better, I'd think that you knew what you were doing.  :)
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17051804
OK, there is nothing stripped in above number, but you know what I mean :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question