?
Solved

Ridding Myself from Trailing Digits on Dollar Amts

Posted on 1998-08-14
4
Medium Priority
?
260 Views
Last Modified: 2010-05-18
I'm trying to get a dollar amount in the format of $xx.xx.  Using the double datatype, when I multiply two numbers, sometimes I get a format of $xx.00000000007.  I know its stupid, but how can I restrict the output to only two digits after the decimal point?
0
Comment
Question by:earlmoore
[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
  • Learn & ask questions
4 Comments
 

Author Comment

by:earlmoore
ID: 1232862
Edited text of question
0
 

Expert Comment

by:awilkins
ID: 1232863
One way to do this would be to ues a long instead of a double to represent your dollar amount.  This long would then represent the total number of cents.
0
 
LVL 16

Expert Comment

by:imladris
ID: 1232864
Whenever I need to deal with dollars, I avoid float and double like the plague. When you multiply and divide, you can easily get results that are ever so slightly off. I have found it far better to stick with int or long. Multiply the dollar values, add 50 and divide by 100 for a rounded result. (Add 0 to truncate, and 99 to round high).
Output can be done with something like:

StringBuffer dv=new StringBuffer(Integer.toString(dlr));
dv.insert(dv.length()-2,'.');
String dlrstr=dv.toString();

This produces the right string, and could be captured in some utility method.


0
 
LVL 2

Accepted Solution

by:
threshold earned 300 total points
ID: 1232865
In Java, JVM stores the double/float into binary code(IEEE 754)
if you assign 1.05 into double a, there is no binary code that can represent 1.05 exactly.
The JVM just choose the nearest one, so you will got 1.0500000000004...

To calculate currency, you should :
1. use the BigDecimal to calculate/print currency explicitly.
2. use the double and DecimalFormat to print double.

String input_string=myTextField.getText(); // maybe you need to skip the '$' first.
double a=(new Double(input_string)).doubleValue();
DecimalFormat format=new DecimalFormat('###,###,###.##');
System.out.println ("Result : "+format.format(a+0.005)); // to display the neighborhood in the format

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

800 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