2

I've tried to cut a float number to get the number with only two digits afer decimal point using this method:

private float two_digits_converter(float f) {
        DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
        symbols.setDecimalSeparator('.');
        DecimalFormat decimalFormat = new DecimalFormat("#.##", symbols);
        return Float.valueOf(decimalFormat.format(f));
    }

But this method doesn't work in all devices. I keep getting this exception on some devices:

java.lang.RuntimeException: 
  at android.app.LoadedApk$ReceiverDispatcher$Args.run (LoadedApk.java:941)
  at android.os.Handler.handleCallback (Handler.java:739)
  at android.os.Handler.dispatchMessage (Handler.java:95)
  at android.os.Looper.loop (Looper.java:145)
  at android.app.ActivityThread.main (ActivityThread.java:6117)
  at java.lang.reflect.Method.invoke (Native Method)
  at java.lang.reflect.Method.invoke (Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1399)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1194)
Caused by: java.lang.NumberFormatException: 
  at java.lang.StringToReal.invalidReal (StringToReal.java:63)
  at java.lang.StringToReal.initialParse (StringToReal.java:164)
  at java.lang.StringToReal.parseFloat (StringToReal.java:323)
  at java.lang.Float.parseFloat (Float.java:306)
  at java.lang.Float.valueOf (Float.java:343)
  at com.great.two_digits_converter (GreatService.java:908)
  at at com.great.GreatService.access$100 (GreatService.java:70)
  at com.great.GreatService$4.onReceive (GreatService.java:433)
  at android.app.LoadedApk$ReceiverDispatcher$Args.run (LoadedApk.java:931)

The exception is on this line: return Float.valueOf(decimalFormat.format(f));

Would be great if you can share some more general methods that works better.

UPDATE:

I ended up doing it this way :

 private float two_digits_converter(float f) {
        String new_s = String.format(Locale.US,"%.2f", f);
        return Float.valueOf(new_s);
    }
WoW
  • 141
  • 1
  • 8
  • 2
    `Caused by: java.lang.NumberFormatException: `. Read the documentation for the methods you're using, and find what cases lead to a NumberFormatException being thrown. – Carcigenicate Aug 26 '17 at 13:27
  • I know that locale can cause this issue but i thought that symbols.setDecimalSeparator('.'); should fix that – WoW Aug 26 '17 at 13:28
  • Float.valueOf knows nothing about the decimal separator you've set. – algrid Aug 26 '17 at 13:31
  • @algrid Float.parseFloat() is another option – WoW Aug 26 '17 at 13:32
  • You can't. See my answer in the duplicate for why not. If you want decimal places you have to use a decimal radix, not floating-point. – user207421 Aug 26 '17 at 13:33
  • @EJP Thanks , see my update on the question and make a comment about what you think – WoW Aug 26 '17 at 17:00
  • @Tonteria24 Thanks i'll try that too but what do you think about the suggested update in my question? – WoW Aug 26 '17 at 17:20
  • 1
    To cut decimals in a float just do : float rounded = ((int)(input * 100)) / 100f; And this question is not a exact duplicate as @EJP marked, in the old question the inquiry was to round, and now WoW ask to cut decimals. – Lluis Felisart Aug 26 '17 at 17:20
  • I think your conversion back and for to String is not needed, my solution is much simpler, it's only a math problem – Lluis Felisart Aug 26 '17 at 17:22
  • @Tonteria No. See my answer in the duplicate for proof. – user207421 Aug 26 '17 at 20:04
  • @WoW You can't have read either my comment or my answer. Floats don't have decimal places. They have binary places. If you want decimal places you must use a decimal radix. `BigDecimal` or `String`. No method that returns `float` or `douboe` can accomplish this. Proof provided. – user207421 Aug 26 '17 at 20:06

0 Answers0