Home > database >  i cant figure out how to round up decimals in kotlin calculator
i cant figure out how to round up decimals in kotlin calculator

Time:10-29

I just started coding in android studio and was creating calculator but now I'm stuck on one problem.

after struggling a lot I figured out how to make so u can use one dot but now I came across another problem which is after addition I cant seem to round up the decimals. when I do additions in decimals sometimes it gives me something like 1.9999999998 and I cant seem to round it up. for the reference I used Table Row in xml. if necessary I can show you what I have written so far. Thanks in advance.

CodePudding user response:

You need String.format(".1f", value). 1,99999 -> 1.99. If you need to round to higher value, please use ceil: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.math/ceil.html

CodePudding user response:

For formatting numbers, you should always be using NumberFormat or similar.

NumberFormat lets you set a RoundingMode which will do what you want.

Or you could be like me and write your own formatter for numbers because the built-in one didn't do what I wanted.

  • Related