1

I want to do calculations with numbers separated by thousands (comma), and the result will be formatted in thousands separated (comma) as well. Example:

var editText1 = **12,520.00**
var editText2 = **52,345.00**
var result = **64,825.00**
//
var editText1 = **12,520**
var editText2 = **52,345**
var result = **64,825.00**

=====================================

I just tried to format the result according to the separation in thousands (comma) of the values that I would receive.

//formats
decimalSymbols = DecimalFormatSymbols(Locale.US)
format="##,###.##"
decimal = DecimalFormat(format, decimalSymbols)
decimal.roundingMode = RoundingMode.CEILING

//Variables that will receive the values
val prov = profit.text.toString().toDouble()
val cust = costs.text.toString().toDouble()
val amort = amortizacoes.text.toString().toDouble()
val jur = interest.text.toString().toDouble()

//Formatting the result in BigDecimal
result val = (prov - cost - amort - jur) * 0.32
val parsed = BigDecimal(result)
val formatResult = decimal.format(parsed)

tax.setText(formatResult.toString())
Samudra Ganguly
  • 637
  • 4
  • 24
  • As the code suggests, the characters used for separating thousands, and for separating decimal places, are locale-dependent. If you're not 100% certain that your code will never have to cope with other locales, it's probably better not to remove commas per se, but to remove whatever character the locale is using. – gidds Jan 14 '23 at 18:34

1 Answers1

-1
  • Simply remove all commas from the string value:

    value= value.replace(",", "")
    
  • Do your calculations

  • And finally, you can use format to decorate and show them with commas, with:

    "%,d".format(value)
    

Tested with JVM and Kotlin v1.8.0.

Here is the playground link: https://pl.kotl.in/pXpev-dei

enter image description here

Code snippet, pasted here:

fun main() {

    var editText1 = "12,520.00";
    var editText2 = "52,345.00";
// var result = **64,825.00**
    editText1 = editText1.replace(",","");
    editText2 = editText2.replace(",","");
    
    var resDouble = editText1.toDouble() * editText2.toDouble();
    val res = "%,f".format(resDouble)
    println(res)
}
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • 1
    I'm pretty sure that `"."` is a typo in the replace method – Samathingamajig Jan 14 '23 at 06:19
  • Thank you. But I can't use "replace" because the inserted numbers are not String, because it converts to Double to be able to do the calculations in my application – Mariano Muendane Jan 14 '23 at 06:48
  • @MarianoMuendane Sir, I guess you are asking for complete solution.. hope the edited answer helps... Please upvote and accept my answer... – Maifee Ul Asad Jan 14 '23 at 06:54
  • @Samathingamajig yes, it was a typo indeed.... hope it gets cleared out now... i was focusing more on approach, but I think he is focusing on direct answers only... – Maifee Ul Asad Jan 14 '23 at 06:56
  • @MarianoMuendane Are you sure you want to be using doubles, or any floating-point type? See [this question](/q/588004/10134209). If you care about exact values, then consider using e.g. BigDecimal instead. – gidds Jan 14 '23 at 18:32
  • The answer I got is working fine, but I'll have a look. thanks! – Mariano Muendane Jan 15 '23 at 05:10