Java - 如何将数字格式化为 2 char 字符串?

如何将 099 之间的 int 格式化为 2 个字符的字符串。例如

4  becomes "04"
36 becomes "36"

提前致谢。

最佳答案

使用 String.format() . 示例:

class Test{
    public static void main(String[]args){
        System.out.println(String.format("%02d", 42)); //42
        System.out.println(String.format("%02d",  7)); //07
    }
}

编辑:

luiscubal 的评论让我思考,所以我想为什么不对这三个替代方案进行基准测试。

import java.text.*;

interface Fmt{
    public String f(int x);
}

class Test{

    static NumberFormat formatter = new DecimalFormat("00");

    static Fmt f1 = new Fmt(){
        public String f(int x){ return ((x<10)?"0":"") + x; }
        public String toString(){return "f1";}
    };

    static Fmt f2 = new Fmt(){
        public String f(int x){ return String.format("%02d", x); }
        public String toString(){return "f2";}
    };

    static Fmt f3 = new Fmt(){
        public String f(int x){ return formatter.format(x); }
        public String toString(){return "f3";}
    };

    public static void main(String[]args){

        Fmt[] fmts = new Fmt[]{f1, f2, f3, f3, f2, f1};

        for (int x : new int[]{7, 42, 99}){

            String s0 = null;
            for (Fmt fmt : fmts)
                if (s0==null)
                    s0 = fmt.f(x);
                else
                    if (!fmt.f(x).equals(s0))
                        System.exit(1);

            System.out.printf("%02d\n", x);

            for (Fmt fmt : fmts){
                String s = null;
                int count = 0;
                System.gc();
                long t0 = System.nanoTime();
                for (int i=0; i<100000; i++){
                    count += fmt.f(x).length();
                }
                long t1 = System.nanoTime();

                System.out.printf("    %s:%8.2fms, count=%d\n",
                    fmt, (t1-t0)/1000000.0, count);
            }

        }
    }

}

我得到的输出是:

07
    f1:   11.28ms, count=200000
    f2:  195.97ms, count=200000
    f3:   45.41ms, count=200000
    f3:   39.67ms, count=200000
    f2:  164.46ms, count=200000
    f1:    6.58ms, count=200000
42
    f1:    5.25ms, count=200000
    f2:  163.87ms, count=200000
    f3:   42.78ms, count=200000
    f3:   42.45ms, count=200000
    f2:  163.87ms, count=200000
    f1:    5.15ms, count=200000
99
    f1:    5.83ms, count=200000
    f2:  168.59ms, count=200000
    f3:   42.86ms, count=200000
    f3:   42.96ms, count=200000
    f2:  165.48ms, count=200000
    f1:    5.22ms, count=200000

事实证明——如果我测试正确的话——SpeedBirdNine 有最有效的解决方案,尽管它的表现很糟糕。所以我修改我的解决方案:

String answer = ((x<10)?"0":"") + x;

回想起来,我认为这是有道理的,因为我最初的解决方案产生了解析格式字符串的成本,而且我假设 Anthony 的格式化程序背后也有某种形式的数据结构,每次都在迭代。

通过保留包含所有 100 个字符串的 String[] 可以预见会更快,但这可能有点过头了...

https://stackoverflow.com/questions/8023699/

相关文章:

angularjs - 使用 AngularJS 指令格式化输入字段,同时保持范围变量不变

c# - 格式化 .NET DateTime "Day",不带前导零

java - Java 中的 StAX XML 格式

css - 如何在 Zurb Foundation 中垂直居中元素?

django - 是否有 django 模板过滤器来显示百分比?

html - 在每个标记属性上换行并在 Visual Studio HTML 代码编辑器中保持对齐

c# - 用年份格式化 TimeSpan

android - 在 Android 的 textview [ Html.fromhtml() ]

c# - 您如何将 { 和 } 放入格式字符串中

r - 如果我已经有 y 刻度,则用逗号(和 K?MM?)格式化 ggplot2 轴标签