如何将数字格式化为2个字符串? [英] Java - How to format a number to a 2 char string?

查看:160
本文介绍了如何将数字格式化为2个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 0 99 之间的整数格式化为2个字符的字符串。例如:

pre $ 4变成了04
36变成了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(){returnf1;}
};

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

static fmt f3 = new Fmt(){
public String f(int x){return formatter.format(x); }
public String toString(){returnf3;}
};
$ b $ public static void main(String [] args){

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

(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\\\
,x);

(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 \\\

fmt,(t1-t0)/1000000.0,count);





code $ pre

我得到的输出是:

$ p $ 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 $ b $ 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个字符串的字符串[] ,但这可能是矫枉过正的...


How would I format an int between 0 and 99 to a string which is 2 chars. E.g

4  becomes "04"
36 becomes "36"

Thanks in advance.

解决方案

Use String.format(). Example:

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

Edit:

luiscubal's comment got me thinking, so I figured why not benchmark the three alternatives.

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);
            }

        }
    }

}

The output I got was:

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

Turns out - if I tested correctly - that SpeedBirdNine had the most efficient solution, albeit badly presented. So I revise my solution to:

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

In retrospect I think it makes sense, given that my initial solution incurred the cost of parsing the format string, and I'd assume Anthony's formatter also has some manner of data structure behind it being iterated every time.

And predictably a lot faster by keeping a String[] containing all the 100 strings, but that's probably overkill...

这篇关于如何将数字格式化为2个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆