Results 1 to 3 of 3
- 10-26-2010, 11:08 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
this programme dont work as I want Help me!!
this is true output of programme /*4.3 (Conversion from kilograms to pounds) Write a program that displays the following table (note that 1 kilogram is 2.2 pounds):
Kilograms Pounds
1 2.2
3 6.6
...
197 433.4
199 437.8*/
but ı wrote
System.out.println("kilogram" + " pound");
for(int i=1;i<=199;i=i+2)
{
System.out.println(i + "" + i*2.2);
}
this is my output
1 2.2
3 6.6000000000000005
5 11.0
7 15.400000000000002
9 19.8
11 24.200000000000003
13 28.6
....
I didnt understand 6.60000000000005 instead of 6.6
or 15.400000000002 instead of 15.4
- 10-26-2010, 11:27 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
When you display a numeric value like i*2.2 on the screen as a string you have to decide on the formatting and other properties of the string.
At the moment you are using the println() method of System.out but there is another method you can use: printf().
It can be a bit tricky to start with, but try
Java Code:System.out.printf("%d %.2f%n", i, i*2.2);
"%d %.2f%n" is a format string. %d means "print a decimal integer", %.2f means "print a floating point number with 2 decimal places", %n means "print a new line".
Full details in the Formatter (Java Platform SE 6) Formatter API docs. There's a lot to digest here - but it's worth reading up on what you need on a particular occasion and going back to it as required.
-----
A completely different approach is to use the DecimalFormat class.
Both approaches are covered in Oracle's Tutorial in the section Formatting Numeric Print Output.
- 10-26-2010, 11:27 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
double calculations are fairly precise, you will have to format the output
String (Java Platform SE 6)
I keep getting beaten to it.....Last edited by al_Marshy_1981; 10-26-2010 at 11:29 PM. Reason: pbrockway explained it
Similar Threads
-
cannot compile the programme
By Roshini in forum New To JavaReplies: 3Last Post: 09-06-2010, 11:02 AM -
Can I embed a console java programme on my site?
By ngc0202 in forum New To JavaReplies: 7Last Post: 08-10-2010, 08:14 PM -
Hello! and I need help. I dont know were to start
By Fall0ut in forum New To JavaReplies: 10Last Post: 05-19-2010, 06:26 PM -
Using libraries that others dont have(j3d)
By scorpion9 in forum Advanced JavaReplies: 7Last Post: 02-19-2010, 11:24 AM -
add java programme on windowsXP right-click menu
By nanaji in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks