【问题标题】:How to use zero as prefix if value less than equals to 9 [duplicate]如果值小于等于 9,如何使用零作为前缀 [重复]
【发布时间】:2014-08-27 15:35:43
【问题描述】:

如果值小于等于9,如何添加零作为前缀,我使用以下方法来实现:

            int countZero = 0;

            if(countVat <= 9)
            {
                countVat = countZero + countVat;
                Log.d("countVat:", String.valueOf(countVat));
            }

但这对我不起作用,如果 countVat 值小于等于 9,仍然会得到个位数

【问题讨论】:

  • 将结果转换成字符串
  • countVat 是 int 类型的变量 @Sharpedge
  • 您正在对两个整数求和。在求和之前将它们转换为字符串。
  • int countZero = 0; if(countVat

标签: java android


【解决方案1】:

使用字符串格式

String.format("%02d", num);

【讨论】:

    猜你喜欢
    • 2014-05-07
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 2019-09-06
    • 2012-09-11
    • 1970-01-01
    相关资源
    最近更新 更多