Solved

Java value conversion

Posted on 2004-09-12
6
200 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 125 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
Custom Java Application connects to database: Which one of the 20? 4 28
tomcat startup error 5 64
runtime exception 2 28
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

773 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