Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format number using decimal format in kotlin

Tags:

I am facing an issue where I need to do some calculations with a number like for example 5000,00 multiplied it by (1,025^3).

So in this case 5000,00 * (1,025^3) = 5385,45

So my question is, how can I format the number 5385,45 to be like 5.385,45 using decimal format maybe?

I tried by myself and I did this piece of code that outputs 5385,45 in the app but not 5.385,45

    var interestValue = (5000,00*(Math.pow(1.025,yearValue)))     val number = java.lang.Double.valueOf(interestValue)     val dec = DecimalFormat("#,00")     val credits = dec.format(number)     vValueInterest.text = credits 
like image 749
José Nobre Avatar asked Dec 19 '18 09:12

José Nobre


People also ask

What is .2f in Java?

printf("%. 2f", value); The %. 2f syntax tells Java to return your variable (value) with 2 decimal places (.

How do you round a double to two decimal places on Android?

You can use String. format("%. 2f", d) , your double will be rounded automatically.


1 Answers

This is the format you need:

val dec = DecimalFormat("#,###.##") 

will print:

5.384,45 

if you need always exactly 2 digits after the decimal point:

val dec = DecimalFormat("#,###.00")  
like image 100
forpas Avatar answered Sep 22 '22 02:09

forpas