舍入到无小数位(返回值为10位小数?)

时间:2014-09-03 12:52:19

标签: java android

所以,我已经实现并运行了这段代码:

try {
        float healthF = Float.parseFloat(getHealth);
        healthchest.setText(Float.toString((healthF * 100) * 0.30f));
    }

但它返回的值大约有10个小数位,最后有6个随机放置。类似于此的东西:###。0000000006。奇怪的是,healthF的值是30. 3000 * .3等于900.不是900.0000000006。

我试图实现这个:

try {
        float healthF = Float.parseFloat(getHealth);
        healthchest.setText(Float.toString((healthF * 100) * 0.30f));
        df.format(healthchest);
    } 

但它会导致应用程序崩溃并使用以下logcat:

09-03 03:26:54.755: W/dalvikvm(1080): threadid=1: thread exiting with uncaught exception (group=0x41465700)
09-03 03:26:54.785: E/AndroidRuntime(1080): FATAL EXCEPTION: main
09-03 03:26:54.785: E/AndroidRuntime(1080): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mikitz.rogsimple/com.mikitz.rogsimple.ArmorStatsSP}: java.lang.IllegalArgumentException: Bad class: class android.widget.EditText
09-03 03:26:54.785: E/AndroidRuntime(1080):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at android.os.Looper.loop(Looper.java:137)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at java.lang.reflect.Method.invokeNative(Native Method)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at java.lang.reflect.Method.invoke(Method.java:525)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at dalvik.system.NativeStart.main(Native Method)
09-03 03:26:54.785: E/AndroidRuntime(1080): Caused by: java.lang.IllegalArgumentException: Bad class: class android.widget.EditText
09-03 03:26:54.785: E/AndroidRuntime(1080):     at java.text.NumberFormat.format(NumberFormat.java:304)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at java.text.DecimalFormat.format(DecimalFormat.java:702)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at java.text.Format.format(Format.java:93)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at com.mikitz.rogsimple.ArmorStatsSP.onCreate(ArmorStatsSP.java:78)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at android.app.Activity.performCreate(Activity.java:5133)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-03 03:26:54.785: E/AndroidRuntime(1080):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-03 03:26:54.785: E/AndroidRuntime(1080):     ... 11 more

2 个答案:

答案 0 :(得分:0)

您无法将edittext传递给DecimalFormat,请使用以下方法将值传递给它,而不是将返回的字符串设置为edittext。

 public static String twoDecimalPlaces(double value){
            try 
            {
                DecimalFormat df = new DecimalFormat("#.##");
                return df.format(value);
            } catch (Exception e) {
                LogHandler.error("twoDecimalPlaces","main",e);
            }
            return String.valueOf(value);
        }

答案 1 :(得分:0)

您只需使用

即可
Float floatValue = 1.454f;
String.format("%.0f",floatValue);