Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Java value conversion

Posted on 2004-09-12
Medium Priority
230 Views
hi everyone,

lets say i got a double value (123.30) i wanna convert it into a Long value (1233000) where the last 4 digits represents the decimal value. issit possible?

thnx
0
Question by:mooy
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 92

Expert Comment

ID: 12041090
try:

double d = 123.30;
DecimalFormat df = new DecimalFormat("0.0000");
String s = df.format(d);
long l = Long.parseLong(s);
0

LVL 6

Expert Comment

ID: 12041142
objects,
the above code gives number format exception.

0

LVL 92

Accepted Solution

objects earned 500 total points
ID: 12041149
oops :) forgot to strip the decimal point out

double d = 123.30;
DecimalFormat df = new DecimalFormat("0.0000");
String s = df.format(d);
int dot = s.indexOf(".");
long l = Long.parseLong(s.substring(0, dot)+s.substring(dot+1));
0

LVL 6

Expert Comment

ID: 12041155
yup!!! thts correct now :)
0

Author Comment

ID: 12050968
hmm.... this is the only way in getting round this?
0

LVL 2

Expert Comment

ID: 12051044
I'm not sure but it looks as if you just want to move the decimal point 4 places to the right and then convert the resulting value to a long.  Is this what you're looking for?

double doubleVal = 123.30;
long longVal = (long) (doubleVal * 10000);

longVal should now be 1233000

I hope that helps.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month5 days, 16 hours left to enroll