• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to cut the double decimal point  RSS feed

 
Simpson Kumar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im getting the double as 1.88706964903238, I dont want to see too long value
I just need to reduce to 1.88 only. How can we do? I tried in Math, but I didn't find
 
arulk pillai
Author
Ranch Hand
Posts: 3417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the BigDecimal class. Try to stay away from double and float.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To display that value you can use the % tags, described here and here. Your number will of course come out as 1.89.

You can multiply by 100, cast to an int and divide by 100.0, if you prefer. That should give 1.88.
 
Pragna Mohapatra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sumant kuchipudi:
Im getting the double as 1.88706964903238, I dont want to see too long value
I just need to reduce to 1.88 only. How can we do? I tried in Math, but I didn't find
 
Pragna Mohapatra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use Math.Round
or math.trim

Math.round will increment the last digit but trim should work fine.ever big decimal you can use with precision 2
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Where is the Math#trim method from? I can't find it in the Sun API. Is it an Apache application?
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Decimal format
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no trim method in DecimalFormat, but it has methods to set rounding mode and number of digits after the decimal point.
 
yashpal waghmare
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit]Added code tags; please use them to maintain indentation. Also altered some indentation. CR[/edit]
[ December 11, 2008: Message edited by: Campbell Ritchie ]
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
There's no trim method in DecimalFormat, but it has methods to set rounding mode and number of digits after the decimal point.


Hi,
I was answering to the OP, not to your question (like you, I never heard of such a thing like Math.trim method)

Cheers,
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by yashpal waghmare:
. . .
double yash5 = Double.parseDouble(yash1);
System.out.println("value of the yash5="+yash5);
. . .


That looks inefficient, and may introduce errors; there is the possibility of printing 1.88999999999999999837563468645825 or 1.890000000000000000000000038746325 or similar. It also won't get the value to 1.88 as was requested. If you are going to use Strings, pass them to a BigDecimal constructor. That will preserve precision.

Manuel Leiria, sorry for the misunderstanding
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!