Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes How to cut the double decimal point Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to cut the double decimal point" Watch "How to cut the double decimal point" New topic
Author

How to cut the double decimal point

Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 260
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


Thanks,
Kumar
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3190
Have a look at the BigDecimal class. Try to stay away from double and float.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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

Joined: Dec 11, 2008
Posts: 5
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

Joined: Dec 11, 2008
Posts: 5
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
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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

Joined: Jul 13, 2007
Posts: 171
Decimal format


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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

Joined: Sep 17, 2008
Posts: 22
[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

Joined: Jul 13, 2007
Posts: 171
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
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to cut the double decimal point
 
Similar Threads
# opposite of
How to use C++ created binary files
Double Buffering Problem
addition accuracy
String concatenation