?
Solved

How can i round a float to just .00

Posted on 2003-03-25
5
Medium Priority
?
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

764 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