Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Number randomly rounding

    Hi, from a book I'm reading, I made a volume calculator using a foreign subroutine. The subroutine (TextIO) can be found by CLICKING HERE

    The code for what I've made is as followed:

    Code:
    
    public class Volume {
    	public static void main(String[] argv) {
    		float squareLength;
    	    float area;
    		float otherDimension;
    		float volume;
    		
    		TextIO.put("Please enter the length of your square cuboid face: ");
    		squareLength = TextIO.getlnInt();
    		
    		area = (float)Math.pow(squareLength,2);
    		System.out.print("Your area is: ");
    		System.out.println(area);
    		
    		TextIO.put("Please enter the length of your other dimension: ");
    		otherDimension = TextIO.getlnInt();
    		
    		volume = area * otherDimension;
    		System.out.print("The volume of this shape is: ");
    		System.out.println(volume);
    		
    	}
    
    }


    Basically, by using TextIO, this system works out the volume by squaring squareLength and then multiplying the area by the other dimension. The system works fine, apart from one problem: whenever you square a decimal number or multiply a decimal number by the area in this system, it randomly rounds the number either down despite the fact I'm using primitive type float.

    I don't want it to change the number at all, I want the system to print the actual number for the area and the volume.

    Thanks

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,711
    Thanks
    41
    Thanked 191 Times in 190 Posts
    If you look @ the getlnInt() it returns an int

    Code:
    public static int getlnInt() { 
          int x=getInt(); 
          emptyBuffer(); 
          return x; 
       }
    and then...
    Code:
    getInt()     { 
          return (int)readInteger(Integer.MIN_VALUE, Integer.MAX_VALUE);
       }
    so you are getting as your most primitive type as an Int which makes the float, not a float.

    *I think

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    If you look @ the getlnInt() it returns an int

    Code:
    public static int getlnInt() { 
          int x=getInt(); 
          emptyBuffer(); 
          return x; 
       }
    and then...
    Code:
    getInt()     { 
          return (int)readInteger(Integer.MIN_VALUE, Integer.MAX_VALUE);
       }
    so you are getting as your most primitive type as an Int which makes the float, not a float.

    *I think
    Thanks for pointing that out, all it involved was just changing the int in the getln subroutine to a float.


  •  

    Tags for this Thread

    Posting Permissions

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