# Rounding and Precesion in Javascript

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?
###### Who is Participating?

Commented:
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>
</form>
0

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

0

Systems architectCommented:
0

Commented:
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

Commented:
exactly!

Good Job lil_puffball
0

Commented:
thank you, yoshi. :)
0

Commented:
sk5567,
Is there any reason for the B? :(
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.