Home arrow static arrow Java Programming [Archive] - Question about decimals and large number values in Java
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Question about decimals and large number values in Java
This topic has 16 replies on 2 pages.    1 | 2 | Next »

Posts:36
Registered: 6/2/04
Question about decimals and large number values in Java  
Jul 13, 2004 8:45 AM



 
I tried to import the number 13915691605 into java but it converted it to 212336
instead. Any number above this is, it seems is just being randomly generated. Any idea how to solve this, should I use Double to define these larger integers?

Also it seems my program can handly only upto 15 decimal digits and my debugging shows rounding occuring for values that have more than 15 digits total, and shows values in exponent format when # of digits prior to decimal position = 16 or more.

please do help in what I should use
 

Posts:6,750
Registered: 1/25/04
Re: Question about decimals and large number values in Java  
Jul 13, 2004 8:46 AM (reply 1 of 16)



 
BigDecimal should cover any bases for you.
 

Posts:24,036
Registered: 2/3/03
Re: Question about decimals and large number values in Java  
Jul 13, 2004 8:47 AM (reply 2 of 16)



 
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
 

Posts:36
Registered: 6/2/04
Re: Question about decimals and large number values in Java  
Jul 13, 2004 8:48 AM (reply 3 of 16)



 
this might sound another newbie question but would that cover all the points!!

any tips on how I can implement it (sheepishly)

thanks
 

Posts:24,036
Registered: 2/3/03
Re: Question about decimals and large number values in Java  
Jul 13, 2004 8:50 AM (reply 4 of 16)



 
any tips on how I can implement it (sheepishly)

http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigDecimal.html
 

Posts:36
Registered: 6/2/04
Re: Question about decimals and large number values in Java  
Jul 13, 2004 8:52 AM (reply 5 of 16)



 
i know about the basic data types.

but i am looking for a method which does not alter the format of the numbers after its been through my program

an example

lets say i import the number 123456789123456789 (18 digit) in the program, I want to output this to my preprogrammed csv file as exactly the same number, without any exponents etc!!

another example, if I import 123456789.123456789
again I want to be able to do the same thing.

I am looking for a common datatye which will allowe me to do both

thanks
 

Posts:11,200
Registered: 7/22/99
Re: Question about decimals and large number values in Java  
Jul 13, 2004 8:55 AM (reply 6 of 16)



 
The value 13915691605 is bigger than the maximum value that can be stored in an "int." You must use "long" variable.

long number = 13915691605L;

Using double is not a very good idea if you insist on having integer values in your program. If you need larger integers than long can provide, have a look at the java.math.BigInteger class.
 

Posts:24,036
Registered: 2/3/03
Re: Question about decimals and large number values in Java  
Jul 13, 2004 8:56 AM (reply 7 of 16)



 
lets say i import the number 123456789123456789 (18
digit) in the program, I want to output this to my
preprogrammed csv file as exactly the same number,
without any exponents etc!!

How are you storing that number in memory? How are you writing it to a CSV file?
 

Posts:24,036
Registered: 2/3/03
Re: Question about decimals and large number values in Java  
Jul 13, 2004 8:57 AM (reply 8 of 16)



 
long number = 123456789123456789L;System.out.println(number);
 

Posts:1,183
Registered: 1/23/02
Re: Question about decimals and large number values in Java  
Jul 13, 2004 8:58 AM (reply 9 of 16)



 
I am looking for a common datatye which will allowe me to do both

BigDecimal does both. However, despite appearances, you can't use it with the normal NumberFormats, since they convert BigDecimal to double and therefore lose precision. If you use constructor BigDecimal(String) and method BigDecimal.soString() you have what you need.
 

Posts:36
Registered: 6/2/04
Re: Question about decimals and large number values in Java  
Jul 13, 2004 9:26 AM (reply 10 of 16)



 
ok

let me explain the program a little bit more.

I input a set of values to a text file. Now these values are extreme values and really can be anything and very large or very small.

I read them into the program in a vector using
vectorresults = ResultSetToVector(testfiledata);

Now I usually transfer them into a csv file, but for simplicity I will just print them out on the screen. And these values seem to have been changed.

to give u a quick example of the value i put in and the value i got out here is a copy paste version

-2147483648.......................-9.22337E+18
-5000000............................. -2.14748E+16
-3132620............................. -2.05299E+11
-1967095036.......................-1967095036
-188119569.........................-188119569
-17952963...........................-17952963
-17093970...........................-17093970
1219770480........................1219770480
1305669820........................1305669820
212336.................................13915691605
2254438...............................1.47747E+11
23855100............................ 1.56337E+12
251658200 ..........................1.64927E+13
-1647313303.......................1.73517E+14
42399993.............................1.82107E+16
44399993..............................1.90697E+17
2147483647.........................9.22337E+18

The right side has the original values, albeit converted into exponent form by java and the right side has the altered values!!

I am trying to get both to be the same
 

Posts:36
Registered: 6/2/04
Re: Question about decimals and large number values in Java  
Jul 13, 2004 9:30 AM (reply 11 of 16)



 
NULL
-9223372036854775808
-21474836481238956
-205299437484
-1967095036
-188119569
-17952963
-17093970
1219770480
1305669820
13915691605
147746850022
1563367840554
16492671809899
173516652089234
18210658604895676
190696520056987258
9223372036854775807

Now this is the list of ORIGINAL data values that I entered. I am hoping to go even more extreme to check the data type intgrity of my program
 

Posts:24,036
Registered: 2/3/03
Re: Question about decimals and large number values in Java  
Jul 13, 2004 9:32 AM (reply 12 of 16)



 
read them into the program in a vector using
vectorresults = ResultSetToVector(testfiledata);

I'd start by looking at this part as to why your values are being changed (quiet failure on overflow error, probably).
 

Posts:24,036
Registered: 2/3/03
Re: Question about decimals and large number values in Java  
Jul 13, 2004 9:37 AM (reply 13 of 16)



 
Another example...
String[] numbers = {"-9223372036854775808",                    "-21474836481238956",                    "-205299437484",                    "-1967095036",                    "-188119569",                    "-17952963",                    "-17093970",                    "1219770480",                    "1305669820",                    "13915691605",                    "147746850022",                    "1563367840554",                    "16492671809899",                    "173516652089234",                    "18210658604895676",                    "190696520056987258",                    "9223372036854775807"}; for (int i = 0; i < numbers.length; i++) {    BigInteger num = new BigInteger(numbers[i]);    System.out.println(num);} // OUTPUT// -9223372036854775808// -21474836481238956// -205299437484// -1967095036// -188119569// -17952963// -17093970// 1219770480// 1305669820// 13915691605// 147746850022// 1563367840554// 16492671809899// 173516652089234// 18210658604895676// 190696520056987258// 9223372036854775807
 

Posts:36
Registered: 6/2/04
Re: Question about decimals and large number values in Java  
Jul 20, 2004 9:10 AM (reply 14 of 16)



 
hi

I would like to alter this, as this worked, now I am trying to implement a vector, so that I can store data from an excel file.

I have used the JACOB COM module to implement this feature.

The table stored in the csv (Excel) is
Letter                 BigNumber                                               StringNumbera                           -9223372036854775808                    -9223372036854775808b                           -21474836481238956                         -21474836481238956c                            -205299437484                                    -205299437484d                           -1967095036                                          -1967095036 e                           -188119569                                           -188119569h                           1219770480                                           1219770480i                             1305669820                                           1305669820m                          16492671809899                                   16492671809899n                            173516652089234                                 173516652089234o                            18210658604895676                            18210658604895676p                            190696520056987258                           190696520056987258q                             9223372036854775807                        9223372036854775807 

to restore this order and setup, I have used a vector implementation and they have been stored in appropriate grids (column i, row j).

Any idea how I can implement this using bigInteger.

The idea for the StringNumber and BigNumber is quite simple, the StringNumber is the String representation of the BigNumber, and is there so that I can check the numbers after the test is completed. The alphabets are there as STRINGS, no CHAR's and they are to see if this method will accept them as well.

I have tried to implement it using JACOB COM which was created by Dan adler, but now am stuck, for those who are interested in the implementation, here is how I have called the data from the excel to the vector:

for(int i = 1; i <= intCols; i++)                        {                                //we're adding to a vector within a vector and the ResultData starts at index 1                                ((Vector)resultVector.elementAt(0)).add(i-1, Dispatch.callN(BIResultData.toDispatch(), "ColumnName", new Variant[]{new Variant(i)}).toString());                        }                         //add data to the vector                        for(int j = 1; j <= intCols; j++)                        {                                for(int i = 1; i <= intRows; i++)                                {                                        //add cell to appropriate place in vector                                        ((Vector)resultVector.elementAt(i)).add(j-1, Dispatch.callN(ResultData.toDispatch(), "Cell", new Variant[]{new Variant(i), new Variant(j)}).toString().toString());                                }                        } 

any further assistance would be very appreciated.

Thanks so far :-)
 
This topic has 16 replies on 2 pages.    1 | 2 | Next »