【问题标题】:Convert field from Json to int and change it value将字段从 Json 转换为 int 并更改其值
【发布时间】:2015-11-22 19:28:05
【问题描述】:

我有按钮“+”和“-”的活动

当我点击“+”时,我的产品数量变为 +1

我有价格,它从 json 解析。

我需要将“price”字段转换为 int 并在点击“+​​”时提高价格。

我该怎么做?

我的活动代码:

private void ParseAndDisplay(JsonValue json)
{
    TextView productname = FindViewById<TextView>(Resource.Id.posttitle);
    TextView content = FindViewById<TextView>(Resource.Id.postcontent);
    TextView price = FindViewById<TextView>(Resource.Id.price);
    TextView weight = FindViewById<TextView>(Resource.Id.weight);
    ImageView imagen = FindViewById<ImageView>(Resource.Id.image1);
    JsonValue firstitem = json[4];


    productname.Text = firstitem["post_title"];
    content.Text = firstitem["post_excerpt"];
    price.Text = firstitem["price"] + " грн";
    weight.Text = firstitem["weight"] + "г";
    var imageBitmap2 = GetImageBitmapFromUrl(firstitem["img_url"]);
    imagen.SetImageBitmap(imageBitmap2);
}

【问题讨论】:

    标签: c# android json xamarin


    【解决方案1】:

    你只需要使用 int.Parse()。 但我猜你需要十进制类型的价格。

    //convert the price to decimal
    var priceValue = decimal.Parse(firstitem["price"]);
    //then increase the price
    priceValue += 1M;
    //then update the UI
    price.Text = string.Format("{0:0.00} грн", priceValue);
    

    【讨论】:

    • 错误 CS0029: 无法将类型 'int' 隐式转换为 'string' 有这个
    • 你在 var price = int.Parse(firstitem["price"]); 行有这个吗?如果你得到错误是因为它已经是一个 int,所以你不需要 int.Parse() :)
    • "price":"39.00" 它在 json 中,据我所知是字符串
    • 我猜问题是当您需要将其转换回 UI 的字符串时。我已经重写了答案。
    【解决方案2】:

    当我们谈论金钱时,您需要根据语言环境(美元、卢比、英镑等)显示金钱,您应该使用 BigDecimal 类。

    例如,如果你在印度,你将使用这个类来代表金钱

    public class Money {
    
        private static final Currency INR = Currency.getInstance(new Locale("en",
                "in"));
        private static final RoundingMode DEFAULT_ROUNDING = RoundingMode.HALF_EVEN;
    
        private BigDecimal amount;
        private Currency currency;
    
        public static Money rupees(BigDecimal amount) {
            return new Money(amount, INR);
        }
    
        Money(BigDecimal amount, Currency currency) {
            this(amount, currency, DEFAULT_ROUNDING);
        }
    
        Money(BigDecimal amount, Currency currency, RoundingMode rounding) {
            this.amount = amount;
            this.currency = currency;
    
            this.amount = amount.setScale(currency.getDefaultFractionDigits(),
                    rounding);
        }
    
        public BigDecimal getAmount() {
            return amount;
        }
    
        public Currency getCurrency() {
            return currency;
        }
    
        @Override
        public String toString() {
            return getCurrency().getSymbol() + " " + getAmount();
        }
    
        public String toString(Locale locale) {
            return getCurrency().getSymbol(locale) + " " + getAmount();
        }
    }
    

    它会将值四舍五入到小数点后 2 位,所有计算都将在科学标签上精确完成。

    为了可以像这样使用上面的类在 UI 上显示钱,它会自动添加卢比符号:-

    ((TextView) rootView.findViewById(R.id.price))
                    .setText(Money.rupees(
                            BigDecimal.valueOf(Long.valueOf(GlobaDataHolder
                                    .getGlobaDataHolder().getProductMap()
                                    .get(productCategoryNumber)
                                    .get(productNumber).getSellMRP())))
                            .toString());
    

    要更新结帐价格,请使用此方法:-

    public void updateCheckOutAmount(BigDecimal amount, boolean increment) {
    
    //update global value of checkout amount
            if (increment) {
                checkoutAmount = checkoutAmount.add(amount);
            } else {
                if (checkoutAmount.signum() == 1)
                    checkoutAmount = checkoutAmount.subtract(amount);
            }
    
    //update displayed checkout amount on UI
            checkOutAmountTextView.setText(Money.rupees(checkoutAmount).toString());
    
        } 
    

    【讨论】:

      猜你喜欢
      • 2018-10-08
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      • 1970-01-01
      • 2013-07-14
      • 2016-01-22
      • 2020-10-17
      相关资源
      最近更新 更多