Solved

# a little bug

Posted on 2008-06-17
205 Views
hi,
I have  a small problem in java, but not in c or c++. I guess the type problem . However, I don't know how to fix. please help.

if the input is 10.19 & 10.31, the result is supposed to be 12. However, I have Pennies are 12.000084.

if i change the line pennies = (amountReceived - amountDue) *100;
to pennies = amountReceived*100 - amountDue *100;

I will get correct answer because values are converted to int before do calculation. I just wonder what if I do some division program. How cold I avoid this problem? Thanks

``````import java.util.*;

import java.io.*;

class Demo

{

public static void main ( String[] args)

{

Scanner temp;

System.out.print("Enter the amountDue : " );

temp = new Scanner( System.in);

amountDue      = temp.nextFloat();

temp = new Scanner( System.in);

float pennies, nickels, dimes, quarters, dollars;

float container;

pennies = (amountReceived - amountDue) *100;

System.out.print("Pennies are " + pennies + "\n");

}

}
``````
0
Question by:valleytech
• 2
• 2

LVL 11

Assisted Solution

spoxox earned 300 total points
Floating point numbers' representation at the machine's level are not exact, so errors like this happen.

A Formatter or System.out.format can help.

See http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html
0

LVL 11

Accepted Solution

spoxox earned 300 total points

System.out.format("Pennies are %4.2f", pennies);
0

LVL 6

Assisted Solution

psychic_zero earned 200 total points
Maybe this is due to you're using float type. Maybe you can use number formatting to make sure the result is 12 by adding this code:

``````pennies = (amountReceived - amountDue) *100;

NumberFormat formatter = new DecimalFormat("##");

String s = formatter.format(pennies);

System.out.print("Pennies are " + pennies + "\n");
``````
0

LVL 6

Expert Comment

To do my way, I think you need to include import java.text.*;
0

Author Comment

and pennies must be s

for future reference

import java.text.*;

pennies = (amountReceived - amountDue) *100;

NumberFormat formatter = new DecimalFormat("##");
String s = formatter.format(pennies);

System.out.print("Pennies are " + pennies + "\n");
0

## Featured Post

### Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.