• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling large numbers in javascript

 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would need to use strings to represent them, since that's beyond what even 64-bit numerical data types can accommodate.
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Parthasarathy balakrishnan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic