荷兰语语言逗号的Android被解释为DOT(。)

发布于 2025-01-31 11:13:22 字数 359 浏览 3 评论 0原文

我正在使用以下代码将用户在EditText中输入的金额进行格式化。

fun String.formatWithComma(): String {
    return try {
        val formatter = DecimalFormat("#,###,###")
        formatter.format(toDouble())
    } catch (ex: Exception) {
        return ""
    }
}

但是,当我将电话语言切换到荷兰语时,逗号将转换为点(。)。就像用户想要键入4,323一样,将其转换为4.323。

我想甚至用荷兰语来逗号而不是点。任何解决方案

I am using the following code to format the amount entered by user in EditText.

fun String.formatWithComma(): String {
    return try {
        val formatter = DecimalFormat("#,###,###")
        formatter.format(toDouble())
    } catch (ex: Exception) {
        return ""
    }
}

But when I switch the phone language to dutch, the comma is converted to dot (.). Like if user wants to type 4,323 , it is converted to 4.323.

I want to comma instead of dot even in dutch language. Any solution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相对绾红妆 2025-02-07 11:13:22

一些欧洲国家的使用方式与美国和英国不同。在使用之前,您应该添加一条额外的行以指示它:

formatter.decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.ENGLISH)

Some European countries use comma and dot in the different way than USA and England. You should add an extra line to instruct it before you use it:

formatter.decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.ENGLISH)
时光磨忆 2025-02-07 11:13:22

我不确定是否可以解决您的问题。

println("${"4.323.001".formatWithComma()}")

如果我们在字符串中找到了多个点,我将替换值,根据您的需求改进该解决方案。

fun String.formatWithComma(): String {
    return try {
        var lastIndexOf = this.lastIndexOf(".");
        var temp = this
        var temp2 = ""
        if(this.split(".").size >= 2)
        {
        temp = this.substring(0,lastIndexOf).replace(".","");
        temp2 = this.substring(lastIndexOf,this.length);    
        }
        
        val formatter = DecimalFormat("#,###,###.###")
        return formatter.format((temp+temp2).toDouble())
    } catch (ex: Exception) {
        return ""
    }
}

I am not sure, if this will fix your problem.

println("${"4.323.001".formatWithComma()}")

If we found multiple dots in the string, I am replacing that values, Improve this solution based on your needs.

fun String.formatWithComma(): String {
    return try {
        var lastIndexOf = this.lastIndexOf(".");
        var temp = this
        var temp2 = ""
        if(this.split(".").size >= 2)
        {
        temp = this.substring(0,lastIndexOf).replace(".","");
        temp2 = this.substring(lastIndexOf,this.length);    
        }
        
        val formatter = DecimalFormat("#,###,###.###")
        return formatter.format((temp+temp2).toDouble())
    } catch (ex: Exception) {
        return ""
    }
}
山人契 2025-02-07 11:13:22

基于 aimin pan

解决此问题的最终解决方案是

fun String.formatWithComma(): String {
    return try {
        val formatter = DecimalFormat("#,###,###")
        formatter.decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.ENGLISH)
        formatter.format(toDouble())
    } catch (ex: Exception) {
        return ""
    }
}

On the basis of answer given by AIMIN PAN

Final solution to fix this issue is

fun String.formatWithComma(): String {
    return try {
        val formatter = DecimalFormat("#,###,###")
        formatter.decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.ENGLISH)
        formatter.format(toDouble())
    } catch (ex: Exception) {
        return ""
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文