如何计算String变量中两个数字的乘法?

时间:2018-01-29 08:33:45

标签: java android types

String numberS = "1,332.0";
String numberE = "10";
    try {
      double value = Double.parseDouble(numberS) * Double.parseDouble(numberE);
      Log.d("result", String.valueOf(value));
    } catch (NumberFormatException e) { }

然后,我想了解String.parseString()String.valueOf()&之间的差异。 toString()

1 个答案:

答案 0 :(得分:2)

首先,你必须从字符串中删除逗号(,)以转换或解析为double或整数,否则它将给出<td>或无效的整数。您可以删除逗号(,),

$rows = substr_count($iname, "\n") + 1;
for ($i = 0; $i < $rows; $i++) {
    echo "<tr>";
    if ($i == 0) { ?>
        <td rowspan='<?php echo $rows;?>' width='25%'><?php echo $ptitle . "<br>" . nl2br($iname);?></td>
    <?php }
    ?>
    <td contenteditable="true" name="v1"></td><td contenteditable="true" name="v2"></td>...
    </tr>
<?php }

以上行将java.lang.NumberFormatException: Invalid double转换为String newNumberS = numberS.replace(",", ""); 。所以,NumberFormatException解决了。现在整个工作,

1,332.0

现在关于1332.0String numberS = "1,332.0"; String numberE = "10"; try { String newNumberS = numberS.replace(",", ""); double value = Double.parseDouble(nNumberS) * Double.parseDouble(numberE); log.i("tag", "value: "+value); } catch (NumberFormatException e) { //catch exception here, if occur. } String.parseString()之间的差异。

因此,如果给定String.valueOf(object)为null Object.toString()将打印出来,而string将抛出String.valueOf(Object)