How to truncate a number from left in Java

1 Answer

0 votes
public class MyClass {
    private static int truncate_left(int num) {
    	int total = (int)Math.log10(num); // total - 1 = 3
    
    	int first_digit = num / (int)Math.pow(10, (total));
    
    	return num - first_digit * (int)Math.pow(10, (total));
    }
    
    public static void main(String args[]) {
        int num = 7483;

	    System.out.print(truncate_left(num));
    }
}






/*
run:
      
483
      
*/

 



answered Jan 10, 2024 by avibootz

Related questions

1 answer 111 views
1 answer 136 views
1 answer 102 views
1 answer 146 views
1 answer 109 views
109 views asked Jan 11, 2024 by avibootz
1 answer 93 views
1 answer 96 views
96 views asked Jan 11, 2024 by avibootz
...