• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

Help with DecimalFormat

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
InquisitiveProgrammer
Asked:
InquisitiveProgrammer
  • 4
  • 2
1 Solution
 
for_yanCommented:
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
 
InquisitiveProgrammerAuthor Commented:
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
 
for_yanCommented:

   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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
InquisitiveProgrammerAuthor Commented:
I found my bug, thanks.
0
 
for_yanCommented:
That  would be really strange

If you just do

System.out.println("$256.00")

does it print dollar sign ?
0
 
for_yanCommented:
Great!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now