Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java value conversion

Posted on 2004-09-12
6
Medium Priority
?
236 Views
Last Modified: 2010-03-31
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
Comment
Question by:mooy
6 Comments
 
LVL 92

Expert Comment

by:objects
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

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

0
 
LVL 92

Accepted Solution

by:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

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

Author Comment

by:mooy
ID: 12050968
hmm.... this is the only way in getting round this?
0
 
LVL 2

Expert Comment

by:bcsonka
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question