Solved

Java Beginner Programming Question

Posted on 2013-01-30
3
258 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
Tomcat: Unable to run tomcat service. 2 23
login form jsp example 2 35
going to wrong jsp page 2 33
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

791 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