• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculation problem  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello again,
I am here with  a new peoblem...
why when i run this code compiler show me
Area =
35.550000000000004
Perimeter =
24.8
the code :

My problem is at Area.
I am a begginer btw.
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the problem you are facing?

If you want Area and its value to be printed in single line, you can do it this way:

 
Radu Catalin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i calculate 4.5 * 7.9 compiler show me 35.550000000000004 instead 35.55.
what ia this 0000000000004? this is my problem
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radu Catalin wrote:when i calculate 4.5 * 7.9 compiler show me 35.550000000000004 instead 35.55.
what ia this 0000000000004? this is my problem


That is because those numbers are multiplied as double values and the resultant is a double value which has that precision. If You want, you can format the number using  String.format("%.2f", (4.5 * 7.9));
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radu Catalin wrote:when i calculate 4.5 * 7.9 compiler show me 35.550000000000004 instead 35.55. . . .
Do you really get that result from the compiler? Surely it is the runtime that shows that result?
If you really need precision use BigDecimal(←link): look at the two links in this old post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!