• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with double  RSS feed

 
Ed Mehm
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

//Defining your variable
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
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

                  //Defining your variable
  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
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leoncio Thiago:

Please UseCodeTags (that's a link) when you post code.  Thank you.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Remember that, sometimes, the toString() conversion uses scientific notation.

And when is sometimes?
The answer is here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!