Solved

Java value conversion

Posted on 2004-09-12
6
206 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 21
Android development question 2 55
mysql jsp example issue 32 51
Java Eclipse Loop 3 21
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

821 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