【问题标题】:how to remove zeros after decmal from string remove all zero after dot如何从字符串中删除小数点后的零删除点后的所有零
【发布时间】:2013-10-31 06:38:10
【问题描述】:

我以1299.00004399.000 这种格式从服务器获取价格,我将如何删除“。”之后的所有零。请帮助我,我将如何做到这一点,如何在“之后删除数字”。 ???我正在获取表单服务器价格值浮动数字我将如何在“。”之后删除所有零

如何删除小数点后的零,如 13400.0000(删除最后 4 个零)

2349.00(删除最后两个零)

删除"."之后的所有零我该怎么做?

static ArrayList<String> Category_price= new ArrayList<String>();
Category_price.add(object.getString("price"));

   holder.txtText3.setText("Price: "+Html.fromHtml(ProductList.Category_price.get(position)));

谢谢

【问题讨论】:

  • 将其设为 int 变量
  • 将其转换为适当的数据类型。如果不允许小数,则使用int/long,如果允许某些小数精度,则使用double(请注意相对精度限制 - 只要没有[或仔细限制]沿值执行数学运算,它应该是“ OK”代表价格)。然后,在显示数据时,使用正确的格式(到字符串)转换来显示小数点后适当的(可能没有)位数。
  • (当然你应该可能设计代码来处理像99.95这样的数据案例 - 该死的偷偷摸摸的方式让100美元看起来更少。)
  • 这适用于c#,可能也适用于android? stackoverflow.com/a/7983459/93647

标签: android


【解决方案1】:

将其转换为整数类型.......................... ....

分割字符串,使用小数点(.)作为分割符。

String val=Html.fromHtml(ProductList.Category_price.get(position));
String val1=val.substring(0,val.indexOf("."));  
holder.txtText3.setText("Price: "+val1);

【讨论】:

    【解决方案2】:

    试试这个方法..

        BigDecimal bd = new BigDecimal(23.086);
        BigDecimal bd1= new BigDecimal(0.000);    
        DecimalFormat df = new DecimalFormat("0.##");    
        System.out.println("bd value::"+ df.format(bd));
        System.out.println("bd1 value::"+ df.format(bd1));
    

    不仅适用于 Bigdecimal 也适用于小数..

    【讨论】:

      【解决方案3】:

      试试这个

      String[] separated = CurrentString.split(".");
      separated[0]; // this will contain "price"
      separated[1]; // this will contain " all your zeros"
      

      【讨论】:

        【解决方案4】:

        试试这个:

        String price = object.getString("price");
        String price = price.substring(0, price.indexOf("."));
        

        如果您的字符串始终采用该格式(2 位、减号、2 位、减号、4 位、空格等),那么您可以使用字符串的substring(int beginIndex, int endIndex) 方法来获得您想要的。

        注意第二个参数是返回子串后字符的index

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-20
          • 1970-01-01
          • 2014-10-21
          • 1970-01-01
          • 2017-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多