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

x
Solved

# How do I convert the number 357.99021 to 357.99?

Posted on 2004-11-05
Medium Priority
248 Views
How do I convert the number 357.99021 to 357.99? Thanks.
0
Question by:jen2483
• 7
• 3

LVL 92

Assisted Solution

objects earned 420 total points
ID: 12510301
to display it to 2 places use DecimalFormat:

double d = 357.99021;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(d));
0

LVL 92

Assisted Solution

objects earned 420 total points
ID: 12510306
You can also use BigDecimal class to control the number of decimal places.
0

LVL 92

Assisted Solution

objects earned 420 total points
ID: 12510308
0

LVL 86

Assisted Solution

CEHJ earned 80 total points
ID: 12510319
double d = 357.99021;
BigDecimal bd = new BigDecimal(Double.toString(d)).setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println(bd);
0

LVL 92

Expert Comment

ID: 12510334
0

LVL 92

Accepted Solution

objects earned 420 total points
ID: 12510345
or to actually round the value use:

double d = 357.99021;
d = Math.round(d * 100.0) / 100.0; // round to 2 decimal places
0

LVL 86

Expert Comment

ID: 12510352

I did - there's information missing from the BigDecimal link you posted
0

LVL 92

Expert Comment

ID: 12510394
> there's information missing from the BigDecimal link you posted

theres nothing missing :-D , it shows how to set the number of decimal places for a big decimal.
Creating the BigDecimal is a no brainer (and you've over complicated it anyway)

BigDecimal bd = new BigDecimal(d);

0

LVL 92

Expert Comment

ID: 12510425
> I did - there's information missing from the BigDecimal link you posted

Additionally, the page included a link showing how to create and use a BigDecimal (if needed).
0

LVL 86

Expert Comment

ID: 12510545
8-)
0

## Featured Post

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month13 days, 9 hours left to enroll