Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rounding and Precesion in Javascript

Posted on 2004-04-28
7
Medium Priority
?
242 Views
Last Modified: 2007-12-19
Hi
I want to format the user entered number with 14 digit precision. I am getting problem if i enter large number and in particular while rounding it.

For Example
123.45 should become 123.45000000000000 This works fine for me.

If I enter 12345.1234567890123456, this is becoming 12345.123456789013 which is wrong.
The number it should show is 12345.12345678901235.

Any Help?
0
Comment
Question by:sk5567
[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
  • 2
  • 2
7 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 10946528
Perhaps this chart helps you to see what floating point numbers mantissa is:
http://babbage.cs.qc.edu/courses/cs341/IEEE-754references.html

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10946539
0
 
LVL 1

Expert Comment

by:yoshi78
ID: 10952422
Just a suggestion as I'm not in tune with what Zvonko is saying.
1234.12345678901234

array = split()
array(1) = array(1) * 10000   // = 1234.568901234
array2 = split(array(1))
array2(1) = array2(1)*10000 // = 5678.901234

until you get to something managable maybe.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
lil_puffball earned 300 total points
ID: 10965133
Check out this example:

<script>
function round(num,n){
  num=(num+'').split('.');
  var dec=num[1];
  if(!dec||dec.length<=n){return num.join('.');}
  while((dec+'').length>n){
    dec=Math.round(dec/10);
    if(dec<10){break;}
  }
  if(n==0){return Math.round(num[0]+'.'+dec);}
  return num[0]+'.'+dec;
}
function format(num,n){
  num=round(num,n);
  num=(num+'').split('.');
  var dec=num[1];
  if(n==0){return num[0];}
  if(!dec){dec='0';}
  while((dec+'').length<n){dec+='0';}
  return num[0]+'.'+dec;
}
</script>

<form>
<input type=text name=num value="12345.1234567890123456" size=30>
<input type=text size=2 value=14 name=n>
<input type=button value="Round" onclick="alert(format(this.form.num.value,this.form.n.value));">
</form>
0
 
LVL 1

Expert Comment

by:yoshi78
ID: 10967279
exactly!

Good Job lil_puffball
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10968476
thank you, yoshi. :)
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10981071
sk5567,
Is there any reason for the B? :(
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

618 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