Solved

Java Beginner Programming Question

Posted on 2013-01-30
3
257 Views
Last Modified: 2013-01-30
Hi, I need help with the following:
public String toString()
	{
		return (description + price * units);
	}

Open in new window


This command will get me the following result with this input (Ex: Description=Jeans, Price=$59.95, Units =12 will get me the following return)
Jacket719.000000001

Open in new window


How would I go about modifying my return command to represent the following:

Jacket $719.00 

Open in new window

Simply adding the string for $ sign and restricting the output to only 2 decimals.

Help?
0
Comment
Question by:nitro55
3 Comments
 
LVL 20

Accepted Solution

by:
n2fc earned 500 total points
ID: 38837852
import java.text.DecimalFormat;
...
DecimalFormat df = new DecimalFormat( "#########0.00");
...
return (description + " $" + df.format(price * units));

Open in new window

0
 
LVL 14

Expert Comment

by:CPColin
ID: 38837856
java.text.NumberFormat.getCurrencyInstance() will return a NumberFormat object that should get you what you want.
0
 

Author Closing Comment

by:nitro55
ID: 38837880
Thanks bud.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Options in Zimbra Server 2 54
servlet example 11 40
tomcat startup error 5 66
jsp login check 12 32
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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

831 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