android - 使用数据绑定(bind)设置千位分隔符格式

标签 android android-resources number-formatting android-databinding

我有一个如下所示的 ViewModel:

public class OrderViewModel {
    double price = 23748.89;
}

以及我将 OrderViewModel 绑定(bind)到的布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="order"
            type="com.example.viewmodel.OrderViewModel"/>

    </data>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{@string/price_format(order.price)}"/>

    </LinearLayout>

</layout>

使用的格式化字符串如下所示:

<string name="price_format">%+.2f $</string>

它很好地添加了 $ 符号并将浮点表示形式减少到 2 位小数。

结果看起来像

1234.89

现在我想用逗号替换小数点并添加千位分隔符,使我的价格看起来像

1.234,89

我可以只使用我的 XML 格式化字符串来完成此操作,还是必须在 java 代码(我的 ViewModel)中进行格式化?

最佳答案

试试这个格式字符串:

%,.2f $

根据自己的发现:

Depending on the Locale of the device (in this case, German ), the desired formatting should be automatically applied.

对于德国语言环境,这将是 1.234,89 $ 而对于英国,它将是 1,234.89 &

关于android - 使用数据绑定(bind)设置千位分隔符格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37674506/

相关文章:

java - Android fragment - 空对象引用

java - TextView 在相对布局中不显示全文

android - getResources().getString() 是如何工作的

android-resources - 扫描Android Studio 3.0中未使用的资源

angularjs - 如何在angularJS中为数字添加千位分隔逗号?

java - 将不同元素居中对齐 - Android

android - 适用于 Android 的 Google 登录 : Cannot resolve RC_SIGN_IN

android - 无法创建特定于版本的值文件夹

java - 如何将 Double 转换为带指数符号的 String

用于表格格式化输出的 C++ 开源库