简体   繁体   English

如何用逗号分隔的数字进行计算?

[英]How to do calculations with numbers separated with commas?

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())
  • 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:最后,您可以使用format来装饰并用逗号显示它们,其中:

     "%,d".format(value)

Tested with JVM and Kotlin v1.8.0.使用 JVM 和 Kotlin v1.8.0 进行测试。

Here is the playground link: https://pl.kotl.in/pXpev-dei这是游乐场链接: https://pl.kotl.in/pXpev-dei

在此处输入图像描述

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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将字符串与逗号分隔的子字符串匹配,确保没有前导/尾随逗号,使用 Kotlin 中的正则表达式 - Match a string with comma-separated sub strings, ensuring no leading/trailing commas, with regex in Kotlin 如何使用随机运算符获得 3 个随机数的结果? - How do I get the result of 3 random numbers using random operator? 如何在 Kotlin 中用空格分隔的同一行上打印? - How to print on the same line separated by space in Kotlin? 如何格式化字符串数字以在 Kotlin 中包含逗号? - How can I format a String number to have commas in Kotlin? 如何检查列表中任何N个项目的总和是否等于或大于Kotlin中的值 - How do I check if sum of any N numbers of items in a list equals or greater than a value in Kotlin 如何在 lambda 表达式中使用 kotlin 中的替换 function 将字母替换为数字 - How do I replace letters with numbers using the replace function in kotlin inside a lambda expression 如何防止数字进入我的 TextView 中的科学记数法? - how do I prevent numbers from going to Scientific Notations in my TextView? RxJava Kotlin如何从单个可观察对象中获取分离的对象<String> - RxJava Kotlin how to get separated objects from single observable<String> 如何在每个元素后将集合转换为带有逗号的数组,而不在 kotlin 中分配新的 memory? - How to transform a Set into an Array with commas after each element without allocating new memory in kotlin? Kotlin:一元加/减运算符对数字有什么作用? - Kotlin: What do the unary plus/minus operators do on numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM