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
5
Medium Priority
?
240 Views
Last Modified: 2010-04-01
how can i round a float to have only 2 places of percision ie to represent money





Thanks in advance
0
Comment
Question by:petergibbions123
  • 3
4 Comments
 
LVL 18

Expert Comment

by:bobbit31
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

by:bobbit31
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

by:
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

by:girionis
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

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question