File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Handling large numbers in javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Handling large numbers in javascript" Watch "Handling large numbers in javascript" New topic
Author

Handling large numbers in javascript

Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
hi
I have a requirement to be able to support large number amounts, upto 23 digits in javascript and then send it to the server for further processing.
Unfortunately all my efforts end up with the number going scientific with the exponential component coming in.

Can someone please tell me how to handle this so that I maintain the integrity of the value?
I want 99999999999999999999999.99 to remain as is, now it gets converted to
1E+23.


Life called,so here I am.<br />Cheers<br />Niki.:-)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42278
    
  64
You would need to use strings to represent them, since that's beyond what even 64-bit numerical data types can accommodate.


Ping & DNS - my free Android networking tools app
Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
how do i convert a number to a string, i mean, when someone enters this into a text box, how do i put this into a string?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42278
    
  64
In JavaScript, everything is a string by default, so if you assign it to a variable there's nothing special you need to do to make it a string.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Originally posted by Ulf Dittmer:
In JavaScript, everything is a string by default

Just to clarify... everything that you enter into a text box is a string.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Parthasarathy balakrishnan
Greenhorn

Joined: Apr 20, 2007
Posts: 14
var x; //is an string variable in javascript
var x=0; //is an integer variable in javascript

any how when you are passing in to database as string/int it will accept both the case to store.

but the problem will occure while when your page get's the value from the database to the front end.


better to implement your code in java side in DAO LAYER to get the values in actual format

below methods returns actual value from 'E' format amt

strAmt is actual value cmg from database as 'E' value and number of decimal points eg [xxx.00 if it is 2]

File name : XXX.class
public static String convertToAmount(String strAmt, int noOfDecimal) {
String ret = strAmt;
if (strAmt == null || strAmt.trim().equals(""))
return strAmt;
try {
double d = Double.valueOf(strAmt).doubleValue();
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(noOfDecimal);
nf.setMaximumFractionDigits(noOfDecimal);
ret = nf.format(d);
} catch (NumberFormatException nfe) {}

return ret;
}


eg: IN JSP IMPLEMENTATION
import the XXX.class file and get the method like given below
<jsp irective.page import="XXX.class"/>
var ival = '<%=XXX.convertToAmount(String.valueOf(Value),2)%>';


correct me if i am wrong.
please implement and see.
it is working for me.



Thanks and Regards,<br />-------------------<br />Parthasarathy Balakrishnan,<br />Bangalore.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Originally posted by Parthasarathy balakrishnan:
var x; //is an string variable in javascript

That is not correct. In this case x is undefined.
Parthasarathy balakrishnan
Greenhorn

Joined: Apr 20, 2007
Posts: 14
thanks for correcting me.

you are right that
var x; // alone will give undefined.

but any how he is going to assign an value to x will not send blank value's to server

whenever there will be the submit button there we have to write an code for
overiding this x value.

x=document.getElementById("XXXamount").value;
submit();

right?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Handling large numbers in javascript