简体   繁体   中英

double and Integer conversion

I am trying to figure this out:

double chiSquare = ((double)(hashtable.get(key).intValue()/noWords))/* * Math.log10((NO_DOCUMENTS/all.get(key)))*/;
if (key.equals("love")){
    System.out.println(hashtable.get(key));
    System.out.println(all.get(key));                
    System.out.println(noWords);    
    System.out.println(chiSquare);
    System.out.println((double)1/841);
    System.exit(0);
}

Why is it printing chiSquare, prints a zero, while printing 1/841 gives the double value? The hashtable is of <String, Integer>

As @GregS indicated in his comment, an int divided by an int is an int. Casting one of the numbers to a double will produce the output your desire.

double chiSquare = ((double)hashtable.get(key).intValue())/noWords;

Or, use the convenience method on Integer to convert it to a double:

double chiSquare = hashtable.get(key).doubleValue()/noWords;

因为括号不正确。

double chiSquare = ( (double) hashtable.get(key).intValue() )/noWords;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM