• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Handling large numbers in javascript

 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic