• 人民币


    以前读书的时候对java基础 理解的不是很深刻,很多东西没有理解和领悟,今天看到了人民币的问题,所以就决定写一个出来,在这里将以前比较简单的进行了升级,包含了小数点后的数据,并进行了四舍五入。

    public class JavaTest09 {
        
        private static final char[] data = new char[]{
            '壹','贰','叁','肆','伍','陆','柒','捌','玖'
        };
        
        private static char[] units = new char[]{
            '分','角','元','拾','佰','仟','万','拾','佰','仟','亿'
        };
        
        public static String convert(Double money){
            StringBuffer buffer = new StringBuffer();
            StringBuffer sbf = new StringBuffer();
            int unit = 2;
            int uniti = 0;
            //对传入的数据进行四舍五入,只保留2位小数
            NumberFormat nFormat = NumberFormat.getInstance();
            nFormat.setMaximumFractionDigits(2);
            //将double类型的数值转换成String型
            String moneyString = nFormat.format(money);
            //money = Double.parseDouble(moneyString);
            //获取小数点坐在的位置
            int index = moneyString.lastIndexOf(".");
            //根据小数点来进行数据分割整数部分
            int Zheng = Integer.parseInt(moneyString.substring(0, index));
            //小数部分
            int Xiao = Integer.parseInt(moneyString.substring(index+1, moneyString.length()));
            //存放小数
            while(Xiao != 0){
                int li = Xiao%10;
                if(li!=0){
                    sbf.insert(0, units[uniti++]);
                    sbf.insert(0, data[li-1]);
                }
                Xiao /= 10;
            }
            //存放整数
            while(Zheng != 0){            
                buffer.insert(0, units[unit++]);
                double number = Zheng%10;
                buffer.insert(0, data[(int) (number-1)]);
                Zheng /= 10;
            }
            sbf.insert(0, buffer);
            return sbf.toString();
        }
        
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            System.out.println(convert(81.2557));
        }
    
    }

    人民币转换

     1 package t0107;
     2 
     3 public class Money {
     4 
     5     private static final char[] data = new char[]{
     6         '零','壹','贰','叁','肆','伍','陆','柒','捌','玖',
     7     };
     8     private static char[] units = new char[]{
     9         '元','拾','佰','仟','万','拾','佰','仟','亿',
    10     };
    11     /**
    12      * @param args
    13      */
    14     public static void main(String[] args) {
    15         System.out.println( convert(12123313) );
    16 
    17     }
    18     
    19     public static String convert(int money){
    20         StringBuffer sbf = new StringBuffer();
    21         int unit = 0;
    22         while(money != 0){
    23             
    24             sbf.insert(0, units[unit++]);
    25             int number = money%10;
    26             sbf.insert(0, data[number]);
    27             money /= 10;
    28         }
    29         return sbf.toString();
    30     }
    31 
    32 }
  • 相关阅读:
    笨办法学Python——学习笔记4
    意识、语言、文字和程序感想
    笨办法学Python——学习笔记3
    把vim作为shell ide
    HDUYuna's confusion 树状数组 Or Multiset
    POJ3252 Round Numbers 组合数学
    HDU3874 Necklace 树状数组+离线处理
    UVA10212 The Last Nonzero Digit. 分解质因子+容斥定理
    HDU1041 Computer Transformation 大数
    HDUFish买电脑 二分查找
  • 原文地址:https://www.cnblogs.com/cfb513142804/p/4209415.html
Copyright © 2020-2023  润新知