Solved

Ridding Myself from Trailing Digits on Dollar Amts

Posted on 1998-08-14
4
224 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
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 100 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 48
mergeTwo  challenge 13 72
countAbc challenge 9 50
Java / Linux and Regular Expressions 11 48
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

14 Experts available now in Live!

Get 1:1 Help Now