Solved

a little bug

Posted on 2008-06-17
5
206 Views
Last Modified: 2010-03-30
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;

		float amountDue, amountReceived;
 

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

		temp = new Scanner( System.in);

        amountDue      = temp.nextFloat();
 

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

		temp = new Scanner( System.in);

		amountReceived = temp.nextFloat();
 

		float pennies, nickels, dimes, quarters, dollars;

		float container;
 

		pennies = (amountReceived - amountDue) *100;

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

	}

}

Open in new window

0
Comment
Question by:valleytech
  • 2
  • 2
5 Comments
 
LVL 11

Assisted Solution

by:spoxox
spoxox earned 300 total points
ID: 21809212
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

by:
spoxox earned 300 total points
ID: 21809217

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

Assisted Solution

by:psychic_zero
psychic_zero earned 200 total points
ID: 21809261
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");

Open in new window

0
 
LVL 6

Expert Comment

by:psychic_zero
ID: 21809265
To do my way, I think you need to include import java.text.*;
0
 

Author Comment

by:valleytech
ID: 21809403
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 38
bitbucket vs gitbucket 3 57
Problem to start Neon 20 86
expectj telnet failing 5 23
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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 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

24 Experts available now in Live!

Get 1:1 Help Now