gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:45:34 26 4
gpt4 key购买 nike

我有一个如下所示的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com