programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Help with double

Ed Mehm
Greenhorn
Posts: 1
Hello, I want from a double number to show only numbers after decimal point.
Example :
double number = 1234.56789;

Output should be :
0.56789

Thank you.

Knute Snortum
Sheriff
Posts: 4279
127
There may be better ways, but try getting the integer part of the number and subtracting it from the original number.  Note: I tried this and the result was inexact, so I rewrote it with BigDecimal and it worked.  Try it with double first; BigDecimal can be daunting.

Piet Souris
Master Rancher
Posts: 2044
75
Since the range of a double is  much larger than the range of even a long, there is no one-to-one relationship between the integer part and a long or integer. A simple way would be to turn the double into a String first, perhaps using a formatter, and then taking the appropriate substring.

Campbell Ritchie
Marshal
Posts: 56546
172
You can do that with the remainder operator. All you have to do is work out what its right operand shou‍ld be. That won't be difficult.

And welcome to the Ranch

Knute Snortum
Sheriff
Posts: 4279
127
Campbell Ritchie wrote:You can do that with the remainder operator. All you have to do is work out what its right operand shou‍ld be. That won't be difficult.

ding, ding, ding!  (light goes on).

Leoncio Thiago
Greenhorn
Posts: 11
Ed Mehm wrote:Hello, I want from a double number to show only numbers after decimal point.
Example :
double number = 1234.56789;

Output should be :
0.56789

Thank you.

They way that I think you might go to is:

Double numberTH = 1234.56789;
//converting it in String
String Str = numberTH.toString();
//then getting everything after the .
System.out.print("Return Value :"+Str.substring(Str.indexOf('.')+1) );

that's my idea. Maybe you will find something better, but this one above works..

I hope it helps you.

Paweł Baczyński
Bartender
Posts: 2083
44
Leoncio Thiago wrote://Defining your variable
Double numberTH = 1234.56789;
//converting it in String
String Str = numberTH.toString();

Why not define the variable as double and use Double.toString(double) static method?
That way there in no unnecessary boxing.

Leoncio Thiago
Greenhorn
Posts: 11
Paweł Baczyński wrote:
Leoncio Thiago wrote://Defining your variable
Double numberTH = 1234.56789;
//converting it in String
String Str = numberTH.toString();

Why not define the variable as double and use Double.toString(double) static method?
That way there in no unnecessary boxing.

Indeed, so something like this might be better:

double numberTH = 1234.56789;
//converting it in String with no unnecessary boxing
String Str = Double.toString(numberTH);
//then getting everything after the .
System.out.print("Return Value :"+Str.substring(Str.indexOf('.')+1) );

Knute Snortum
Sheriff
Posts: 4279
127
Leoncio Thiago:

Please UseCodeTags (that's a link) when you post code.  Thank you.

Henry Wong
author
Sheriff
Posts: 23295
125
Leoncio Thiago wrote:
that's my idea. Maybe you will find something better, but this one above works..

Be careful, as this technique will not work for some values of float / double. Remember that, sometimes, the toString() conversion uses scientific notation.

Henry

Paweł Baczyński
Bartender
Posts: 2083
44
Henry Wong wrote:Remember that, sometimes, the toString() conversion uses scientific notation.

And when is sometimes?
The answer is here.