Results 1 to 2 of 2
  1. #1
    silvia is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default Help with java Rounding

    Hi, I want to get my Java application to be able to round double varibles, but I do not want to use math.round(), I want to do this another way...

    I was thinking of maybe dividing the double variable let say i have

    Java Code:
    double z;
    then in an if statement dividing the input z by one but only retaning the remainder z%1, the if the remainder is less than .49 print (int)z, or something not sure what way to go here, and if the remainder is greater than .50 then z++. I am still not sure of how to actually set this up.

    Thanks

  2. #2
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    U already set up the logic nicely, maybe this how the program would look in its java source code
    Java Code:
    import java.util.*;
    
    public class Rounder
    {
    	public static void main(String[] args)
    	{
    		double z;
    		Scanner scan = new Scanner(System.in);
    		z = scan.nextDouble();
    		if((z%1)>=.50)
    		{
    			z++;
    		}
    		System.out.println((int)z);
    	}
    }
    already tried it, and it works, hope this helps :D

Similar Threads

  1. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 12:04 AM

Posting Permissions

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