Results 1 to 4 of 4

Thread: [Java] How to truncate a float correctly?

Threaded View

  1. #1
    Join Date
    Jun 2009
    Location
    Land of Paranoia and Guns
    Beans
    194
    Distro
    Ubuntu 12.10 Quantal Quetzal

    [Java] How to truncate a float correctly?

    I am by-in-large a python programmer, so I thought it would be a good idea to get some practice in a statically-typed language. I've been learning basic Java, so to test out what I've learned so far I decided to make the obvious joke and make a coffee-based class named Java. Har har har.
    My question has to do with this particular bit of code:
    Code:
    float pct = delta/capacity;
    int heatdelta = 180*pct;
    //int heatdelta = 180 * (delta/capacity);
    heat+=heatdelta;
    The basic idea is to calculate a percentage, then truncate the resulting float. If I use the above code, I get the following error:
    Code:
    main.java:94: error: possible loss of precision
                            int heatdelta=180*pct;
                                             ^
      required: int
      found:    float
    1 error
    However, if I use the commented out line (notice that it is the same operations condensed into 1 line) it compiles fine and behaves as expected.

    Here is the rest of the code sans the main class I use to test the Java class:
    Code:
    class JavaException extends Exception {
        private String msg;
        public JavaException(String err) {
            super(err);
            msg=err;
        }
        public String getError() {
            return msg;
        }
    }
    
    class HotException extends JavaException {
        private String msg;
        public HotException(String err) {
            super(err);
            msg=err;
        }
        public HotException() {
            super("Ouch!");
            msg="Ouch!";
        }
        public String getError() {
            return msg;
        }
    }
            
    class ColdException extends JavaException {
        private String msg;
        public ColdException(String err) {
            super(err);
            msg=err;
        }
        public ColdException() {
            super("Eww!");
            msg="Eww!";
        }
        public String getError() {
            return msg;
        }
    }
    
    class Java {
    
        private int ounces;
        private int capacity;
        private int heat=180; //in F
    
        public void cool() {
            heat-=20;
        }
    
        public void drink(int amount) throws HotException, ColdException {
            int delta=0;
            System.out.println("Mmm, caffeine!");
            if (ounces == 0) {
                System.out.println("What? None left? Preposterous!");
                return;
            }
            if (amount == 0) {
                delta=ounces;
                ounces=0;
            } else {
                delta=amount;
                ounces-=delta;
                if (ounces < 0) {
                    delta+=ounces;
                    ounces=0;
                }
            }
            if (heat > 160) {
                //System.out.println("Ow, this java is hot!");
                throw new HotException();
            } else if (heat < 100) {
                //System.out.println("Eww, this java is lukewarm!");
                throw new ColdException();
            }
            if (heat > 70) {
                heat-=10;
            }
            System.out.println(delta + " ounce(s) consumed");
            System.out.println(ounces + " ounce(s) remaining");
        }
    
        public void refill() {
            if (ounces == capacity) {
                System.out.println("Your coffee is already full, sir...");
                return;
            }
            int delta=capacity-ounces;
            if (delta == capacity) {
                heat=180;
            } else {
                float pct = delta/capacity;
                int heatdelta = 180*pct;
                //int heatdelta = 180 * (delta/capacity);
                heat+=heatdelta;
            }
            ounces=capacity;
            System.out.println("Here you go, sir. Enjoy.");
        }
    
        public Java(int iounces) {
            ounces=iounces;
            capacity=iounces;
        }
    
    }
    So, what is the proper way to truncate a float to an int?
    Last edited by epicoder; October 5th, 2012 at 03:30 PM. Reason: Forgot to actually ask the question. Whups.
    Don't use W3Schools as a resource! (Inconsequential foul language at the jump)
    Open Linux Forums (More foul language, but well worth it for the quality of support and good humor.)
    If you want to discuss W3Schools, please PM me instead of posting.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •