Solved

Help with DecimalFormat

Posted on 2011-10-01
6
166 Views
Last Modified: 2012-05-12
I am trying to append a "$" in front of my number, but for some reason it isn't being added.

Here is a code snippet:

 
// Round the number to two decimal places, then format it properly.
    public String roundTwoDecimals(double number) {
    	DecimalFormat twoDecForm = new DecimalFormat("$0.00");
    	String dollarAmount = twoDecForm.format(number);
    	return dollarAmount;
    }

Open in new window

0
Comment
Question by:InquisitiveProgrammer
  • 4
  • 2
6 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36897328
check this:

        DecimalFormat fff = new DecimalFormat("$#####.00");
        System.out.println(fff.format(345.6789));

               System.out.println(fff.format(365.0));

        System.out.println(fff.format(365));

Open in new window


Output:
$345.68
$365.00
$365.00

Open in new window

0
 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 36897352
It still isn't adding the $ sign.

I'm wondering if it might be something in my Eclipse environment that isn't allowing it to show.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36897361

   public static String roundTwoDecimals(double number) {
    	DecimalFormat twoDecForm = new DecimalFormat("$0.00");
    	String dollarAmount = twoDecForm.format(number);
    	return dollarAmount;
    }

    public static void main(String[] args) {

                  System.out.println("  output: " + roundTwoDecimals(365));

}
}

Open in new window



  output: $365.00

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 36897365
I found my bug, thanks.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36897369
That  would be really strange

If you just do

System.out.println("$256.00")

does it print dollar sign ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36897371
Great!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 46
ForLoop Example 3 52
web application structure 18 95
Java: anonymous class 4 29
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

789 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