Solved

Java value conversion

Posted on 2004-09-12
6
176 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
Comment Utility
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
Comment Utility
objects,
the above code gives number format exception.

0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 6

Expert Comment

by:expertmb
Comment Utility
yup!!! thts correct now :)
0
 

Author Comment

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

Expert Comment

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now