Number to do unit--java

Source: Internet
Author: User

Code
    /**     * 将一个数字处理为以万为单位的字符串,保留两位小数     *     * @param num     * @return     */    public static String getTenThousandOfANumber(Integer num) {        if (num < 10000) {            return String.valueOf(num);        }        String numStr = new DecimalFormat("#.00").format(num / 10000d);        String[] ss = numStr.split("\\.");        if ("00".equals(ss[1])) {            return ss[0] + "万";        } else if (‘0‘ == (ss[1].charAt(1))) {            return ss[0] + "." + ss[1].charAt(0) + "万";        } else {            return numStr + "万";        }    }


Test Cases
Import java.text.decimalformat;/** * @author yawei Xi * @since 2018-7-31 11:40 */public class Test {public static void        Main (string[] args) {int a = 9999;        int B = 10000;        int c = 10001;        int d = 11000;        int e = 11100;        int f = 100000;        System.out.println (Gettenthousandofanumber (a));        System.out.println (Gettenthousandofanumber (b));        System.out.println (Gettenthousandofanumber (c));        System.out.println (Gettenthousandofanumber (d));        System.out.println (Gettenthousandofanumber (e));    System.out.println (Gettenthousandofanumber (f)); /** * Handles a number as a string in million, leaving two decimal places * * @param num * @return */public static string Gettenthousand        Ofanumber (Integer num) {if (num < 10000) {return string.valueof (num);        } String numstr = new DecimalFormat ("#.00"). Format (num/10000d);        string[] ss = numstr.split ("\ \"); if ("xx". Equals (Ss[1])) {return SS[0] + "million";        } else if (' 0 ' = = (Ss[1].charat (1))} {return ss[0] + "." + Ss[1].charat (0) + "million";        } else {return numstr + "million"; }    }}


Test results
99991万1万1.1万1.11万10万

Number to do unit--java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.