Solved

Help with DecimalFormat

Posted on 2011-10-01
6
165 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to track down Infection in a Server 2008 domain user profile 7 51
print map entry 34 67
tomcat not starting 6 45
Why my table column Id is not passed to java object? 4 38
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now