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 115 views
1 answer 139 views
1 answer 108 views
1 answer 148 views
1 answer 112 views
112 views asked Jan 11, 2024 by avibootz
1 answer 94 views
1 answer 99 views
99 views asked Jan 11, 2024 by avibootz
...