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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
Round
Jackie Wang
Ranch Hand
Posts: 315
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
how to round a double to a 2 decimal place?
I am using jdk1.3
Thanks!
Pablo Gomes Ludermir
Greenhorn
Posts: 12
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You can combine those methods in java.lang.Math class:
double ceil(double)
double floor(double)
int round(float)
long round(double)
[]s
Pablo
SCJP14
John Hembree
hired gun
Posts: 250
I like...
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You can use a BigDecimal also like this:
import java.math.BigDecimal; class Temp { public static double getRoundedDouble(double d, int scale) { BigDecimal big_decimal = new BigDecimal(d); return big_decimal.setScale(scale,BigDecimal.ROUND_HALF_DOWN).doubleValue(); } public static void main( String args[]) { double d = Double.valueOf(args[0]).doubleValue(); int s = Integer.parseInt(args[1]) ; System.out.println( getRoundedDouble( d , s ) ) ; } }
Then you can turn this into a function that you call with any number and a decimal place that you want returned: getRoundedDouble( 123.3256 , 2 ) would return 123.33
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Google interview questions for java developer
Its Moose Day!
function
JAVA
Query
More...