如何在Java中将formatter设置为变量?

时间:2019-11-11 08:36:05

标签: java android format

我想格式化Java中的双精度变量,具体取决于存储在另一个双精度变量“ i”中的数字。假设如果i = 3,那么我要格式化

decimals_text = String.format("%."+String.valueOf(3)+"f", 2.123);

anf,如果i = 5,则

decimals_text = String.format("%."+String.valueOf(5)+"f", 2.123);

问题是,如果我使用String.valueOf(3)而不是String.valueOf(i),则会出现错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rpncalculator, PID: 376
java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.example.rpncalculator/com.example.rpncalculator.MainActivity}:   java.util.UnknownFormatConversionException: Conversion = '.'
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.util.UnknownFormatConversionException: Conversion = '.'
    at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2781)
    at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2811)
    at java.util.Formatter$FormatSpecifierParser.<init>(Formatter.java:2624)
    at java.util.Formatter.parse(Formatter.java:2557)
    at java.util.Formatter.format(Formatter.java:2504)
    at java.util.Formatter.format(Formatter.java:2458)
    at java.lang.String.format(String.java:2770)
    at com.example.rpncalculator.MainActivity.onCreate(MainActivity.java:134)
    at android.app.Activity.performCreate(Activity.java:7009)
    at android.app.Activity.performCreate(Activity.java:7000)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

该问题如何解决?

2 个答案:

答案 0 :(得分:0)

您可以改用BigDecimal

BigDecimal bd = new BigDecimal("2.133");
int roundTo = 2;

bd = bd.setScale(roundTo, RoundingMode.HALF_EVEN);
System.out.println(bd);

答案 1 :(得分:0)

您收到错误消息是因为当您应该给double时给了int

您应先将double舍入以转换为int。例如,您可以使用Math.round

您可以先构建格式(使用String.format),然后使用该格式获取字符串。

double i = 3f;
String decimals_format = String.format("%%.%df", Math.round(i)); //for i=3 should give "%.3f"
String decimals_text = String.format(decimals_format, 2.123);
System.out.println(decimals_text);