Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# How can i round a float to just .00

Posted on 2003-03-25
Medium Priority
240 Views
how can i round a float to have only 2 places of percision ie to represent money

0
Question by:petergibbions123
• 3

LVL 18

Expert Comment

ID: 8205185
you could use BigDecimal class, ie:

BigDecimal bd = new BigDecimal(12345.12345);
bd.setScale(2, BigDecimal.ROUND_HALF_UP);
out.println(bd.doubleValue());
0

LVL 18

Expert Comment

ID: 8205229
you can also use DecimalFormat:

DecimalFormat fmt = new DecimalFormat("0.00");
out.println(fmt.format(12345.12345));

and btw, the imports:
java.math.BigDecimal
java.text.DecimalFormat

respectively
0

LVL 18

Accepted Solution

bobbit31 earned 300 total points
ID: 8205259
sorry for the multiple posts, but if you are using BigDecimal, this would be better:

double d = 12345.12345;
BigDecimal bd = new BigDecimal(d);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
out.println (bd.doubleValue());
0

LVL 35

Expert Comment

ID: 10060842
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to bobbit31

girionis
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ready to kick start your career in 2018? Add app developer skills to your resume. January’s Course of the Month features Android App Development training with hands-on learning.  Read on to learn why these skills are important.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
Integration Management Part 2
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
###### Suggested Courses
Course of the Month10 days, 2 hours left to enroll