取得一个数的个位,十位,百位或千位

给定一个数值98,取个位:98%10=8,取十位上的数值:98/10%10=9,通用方法:

 public static void getUnit(int number){
         //将数值转换位字符串,取得它的长度
         String num=number+"";
         for(int i=0,n=1;i<num.length();i++,n*=10){
             //第一次循环的时候,取的是个位,第二次位是十位,第三次位百位…
             System.out.println(number / n % 10);
         }
}

————————————————
原文链接:https://blog.csdn.net/qq_43314793/article/details/90106954

猜你喜欢

转载自www.cnblogs.com/cosmos-wong/p/11838317.html