Solved

Ridding Myself from Trailing Digits on Dollar Amts

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
throw exception 21 69
pagenation logic how it is working in my code 1 71
eclipse console opening separately 2 39
How to fix  socket closed error 11 33
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

696 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