Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Help with DecimalFormat

Posted on 2011-10-01
Medium Priority
172 Views
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;
}
``````
0
Question by:InquisitiveProgrammer
[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
• 4
• 2

LVL 47

Accepted Solution

for_yan earned 2000 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));
``````

Output:
``````\$345.68
\$365.00
\$365.00
``````
0

LVL 1

Author Comment

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

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));

}
}
``````

``````  output: \$365.00
``````
0

LVL 1

Author Comment

ID: 36897365
I found my bug, thanks.
0

LVL 47

Expert Comment

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

ID: 36897371
Great!
0

## Featured Post

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month8 days, 17 hours left to enroll