• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting from double to int leading to in correct data for very specific numbers

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We just came across the following thing, which is very interesting and misleading the other program by its outcome. Could you please tell me, what is wrong with the following little program.


public class CastingClass {

static double x = 269.28;
public static void printValue(){
System.out.println("Original Value: " + x);
System.out.println("Before Casting; " + x * 100);
int i = (int) (x * 100);
System.out.println("After Casting: " + i);
}
static public void main(String[] args){
printValue();
}
}



// I am getting the following result, which is not correct.

-------------------------------------------------
Original Value: 269.28
Before Casting, but multiplying with 100: 26927.999999999996
After Casting: 26927

---------------------------------------------------------
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

H Und wrote:We just came across the following thing, which is very interesting and misleading the other program by its outcome. Could you please tell me, what is wrong with the following little program.


public class CastingClass {

static double x = 269.28;
public static void printValue(){
System.out.println("Original Value: " + x);
System.out.println("Before Casting; " + x * 100);
int i = (int) (x * 100);
System.out.println("After Casting: " + i);
}
static public void main(String[] args){
printValue();
}
}



// I am getting the following result, which is not correct.

-------------------------------------------------
Original Value: 269.28
Before Casting, but multiplying with 100: 26927.999999999996
After Casting: 26927

---------------------------------------------------------




Take a look at point #20 here... https://coderanch.com/how-to/java/JavaBeginnersFaq
 
reply
    Bookmark Topic Watch Topic
  • New Topic