首页 > 其他 > 详细

barcode EAN13 12位计算出最后一位

时间:2021-05-31 21:38:27      阅读:26      评论:0      收藏:0      [点我收藏+]
    /**
     * 计算barcode最后一位
     * @param code 12位的barcode
     * @return
     */
    public static String padBarcode(String code){
        if( null == code || code.length() != 12 ){
            throw new NullPointerException("code 的位数不对!");
        }
        //补上位数 code
        String code_ = code+"0";
        int totalEvent = 0;
        int totalCardinal = 0;
        
        char[] resolve = code_.toCharArray();
        for( int i = 1; i <= resolve.length; i++ )
        {
            int temp = Integer.parseInt((resolve[i-1])+"");
            if( i % 2 == 0 ){
                totalEvent += temp;//偶数和
            }else{
                totalCardinal += temp;//基数和
            } 
        }
        int x = ( totalEvent * 3 ) + totalCardinal;
        
        //取x的值   10最小整数倍的数
        int all = x; 
        if( x % 10 != 0 ){
            all = ( (x / 10 +1) * 10);
        }
        return code+(all-x);
    }

 

barcode EAN13 12位计算出最后一位

原文:https://www.cnblogs.com/Mr-Y1907/p/14831877.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!