473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to NOT leave the javascript convert my number to exponential notation?

I hava a number too large and when i plus it with another number the result is showed in exponential notation on my screen.

how to not leave the javascript do that?

Thanks
Sep 10 '10 #1
2 11852
mrhoo
428 256MB
You are probably better off with the exponential notation.

Javascript loses precision after 15 or so digits.

9284476546528123456781 may be rounded to
9284476546528124000000, and

0.00000000009284476546528123456781 to
0.00000000009284476546528124

If you need more precision you can use a javascript big integer library,
or hand off the calculations to a library on the server.

If you just want to replace the notation with the rounded number,
you can do it with a replace operation on the string value of the number.

Numbers smaller than zero will be returned with the zeroes in front of the first significant digit,
big numbers will add zeroes to the end.


Expand|Select|Wrap|Line Numbers
  1. Number.prototype.todigits= function(){
  2.     var tem='', z, d, s= this.toString(),
  3.     x= s.match(/^(\d+)\.(\d+)[eE]([-+]?)(\d+)$/);
  4.     if(x){
  5.         d= x[2];
  6.         z= (x[3]== '-')? x[4]-1: x[4]-d.length;
  7.         while(z--)tem+='0';
  8.         if(x[3]== '-'){
  9.             return '0.'+tem+x[1]+d;
  10.         }
  11.         return x[1]+d+tem;
  12.     }
  13.     return s;
  14. }

Expand|Select|Wrap|Line Numbers
  1. //TEST
  2. var n= 0.00000000009284476546528123456781,
  3. n2= 9284476546528123456781;
  4. alert(n+'\n'+n.todigits()+'\n'+ n2+'\n'+n2.todigits())
  5.  
  6. returned value: (String):
  7. 9.284476546528124e-11
  8. 0.00000000009284476546528124
  9. 9.284476546528124e+21
  10. 9284476546528124000000
Sep 14 '10 #2
Dormilich
8,658 Expert Mod 8TB
Javascript loses precision after 15 or so digits.
all IEEE 754 floats do that. (that is the only number type in JavaScript)
Sep 14 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ryoga | last post by:
How can I print out the value of a large double in plain decimal form without the exponential notation? For example (quick code): PlainPrint.java: --------------- public class PlainPrint {...
4
by: Timothy Fitz | last post by:
Why are all numberical literals in exponential notation floats? To me it is counter-intuitive for 1e3 to behave so fundamentally different from 1000. Timothy Fitz
9
by: J.Sperlhofer | last post by:
Good morning, Javascript-Professionals. I'm looking for an possibility to show a (calculated) 64bit-Number without exponential notation. I don't want to see exponational notation within my...
3
by: Zion Zadik | last post by:
Hi all, I have strings representing Scientific (exponential) values, for example 5.866000000000000e-002. I need to convert them to double type. How ca this be done without analyzing the string....
5
by: revansx | last post by:
Hi folks, I am programming a page that displays scientific data retrieved from a data source to an asp-web page and i would like to force all number to be formatted/displayed in scientific...
6
by: dumbkiwi | last post by:
Hi, I'm working on a script to download and parse a web page, and it includes xml symbol notation, such as ' for the ' character. Does anyone know of a pre-existing python script/lib to convert...
2
by: mktilu | last post by:
hi can any one tell do we have any function to convert number to text.For example 25 to be converted to twenty five in SQL
2
by: nineballssj9 | last post by:
#include <stdio.h> #include <string.h> #include <math.h> //i'm trying to convert number into word like for an example if i type //99.99 then it will print ninety nine point ninety nine //can...
4
by: b4ukiran | last post by:
Hi all, I have a requirement to print large double values without the exponential notaion. The double value can be a declared variable or any calculated value within the code. In any case, I...
3
ashsa
by: ashsa | last post by:
Hi everyone, I am trying to display a numeric value fetched from an sql server table which is stored in the exponential notation. For eg, 0.08 is getting stored in the table as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.