how do i round/trucate a number without using methods like math.round or %3f?

Posted by user2923875 on Stack Overflow See other posts from Stack Overflow or by user2923875
Published on 2013-10-26T21:25:01Z Indexed on 2013/10/26 21:54 UTC
Read the original article Hit count: 222

Filed under:

So far I need to round a number that I inputted and get it to 3 decimal places without those methods.

    if(number !=(int)number){           
        number*=1000;      
        number=(int)number;  
        number=(double)number; 
        number/=1000;  
        System.out.println("-"+ number);         
    }
    if(number ==(int)number){       
        System.out.println("-"+ number + "00");
    }       

With that above, it will work for any input except the ones with 2 decimal places, like 12.34 . How do I make it work if i type 12.34 and displays 12.340?

© Stack Overflow or respective owner

Related posts about java