Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Paul Clapham
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Saloon Keepers:
Tim Holloway
Carey Brown
Roland Mueller
Piet Souris
Bartenders:
Forum:
Beginning Java
Java (Precision Problem)
Pulkit Malhotra
Ranch Hand
Posts: 30
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi ,
I'm having a float variable which displays value as 12.3333333333 i want it to show only upto 2 decimal places like 12.33.
Can you please tell me is there any direct function for this in math or util package ?
Thanks in advance.
Regards,<br />Pul_Mal
Christophe Verré
Sheriff
Posts: 14691
16
I like...
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You'll find interesting things in the BigDecimal class.
[My Blog]
All roads lead to JavaRanch
Jesper de Jong
Java Cowboy
Posts: 16084
88
I like...
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
So you want to format a number to show only 2 decimal places. You could do that like this:
double value = 12.33333333; String text = String.format("%.2f", value); System.out.println(text);
Or you could use java.text.DecimalFormat:
double value = 12.33333333; DecimalFormat fmt = new DecimalFormat("0.00"); String text = fmt.format(value); System.out.println(text);
[ May 25, 2007: Message edited by: Jesper Young ]
Jesper's Blog
-
Pluralsight Author Page
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Setting precision for a double value
Precision loss in String to Float and double to float
Round a double to 2 places of Decimal
BigDecimal in EL addess scales
Single precision /double precision?
More...