Java中字符串相乘

发布于 2024-10-10 16:02:01 字数 140 浏览 0 评论 0原文

我有一个包含数字的字符串,我想将该数字与 1.28 相乘,

这是字符串的分配方式

String PRICE = dataRecord.get( "PRICE" );

I have a String which contains a number and I would like to multiply that number with 1.28

Here is how the string is assigned

String PRICE = dataRecord.get( "PRICE" );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

情深缘浅 2024-10-17 16:02:01

您需要获取数据的数字表示形式并对其执行乘法。由于示例是“价格”,我假设涉及金钱,因此建议使用小数类型。

String price = "9.99";
BigDecimal priceDecimal = new BigDecimal( price );
BigDecimal total = priceDecimal.multiply( new BigDecimal( "1.28" ) );
System.out.println( total );

You need to obtain a numeric representation of the data and perform a multiplication on it. Since the example is "price", I'm assuming money is involved, and will therefore recommend a decimal type.

String price = "9.99";
BigDecimal priceDecimal = new BigDecimal( price );
BigDecimal total = priceDecimal.multiply( new BigDecimal( "1.28" ) );
System.out.println( total );
笛声青案梦长安 2024-10-17 16:02:01

更好的方法是使用具有双字段类型的适当对象。

double price = dataRecord.price * 1.28;

类似于 @Rob 的示例,它给出了相同的答案。您决定哪个更简单。 ;)

double price = 9.99;
double total = price * 1.28;
System.out.printf("%.4f%n", total);

为了金钱,我建议使用 double 并进行适当的舍入。

A better way would be to use a proper object with a double field type.

double price = dataRecord.price * 1.28;

Similar to @Rob's example which gives the same answer. You decide which is simpler. ;)

double price = 9.99;
double total = price * 1.28;
System.out.printf("%.4f%n", total);

For money, I recommend using double with appropriate rounding.

岁月静好 2024-10-17 16:02:01

您真正想要的是:

double price = dataRecord.getDouble( "PRICE" );

假设 dataRecord 是 ResultSet 对象。无论如何,使用 BigDecimals 会好得多,并且,如果 PRICE 确实存储在 String 对象中,那么我假设您这样做是因为您知道浮点精度问题。

What you really want, is:

double price = dataRecord.getDouble( "PRICE" );

Assuming that dataRecord is a ResultSet object. In any way, It would be much much better to use BigDecimals, and, if PRICE is really stored in a String object, then I assume you did this because you know about the float-precision problem.

╰◇生如夏花灿烂 2024-10-17 16:02:01

将字符串转换为双精度型以进行乘法。 Double.parseDouble() 是您可以使用的方法。我选择将其包装在 try 块中,以防字符串格式错误并引发错误(如果价格由于某种原因不是数字 - 例如 5.67b 或 8..34)。

String price = dataRecord.get( "PRICE" );
try {
    double p = Double.parseDouble(price);
    double result = p*1.28;
    System.out.println(result);
} catch(NumberFormatException e) { // ERROR HANDLING
    System.out.println("Error parsing string: Price is not a number.");
}

作为您的参考,这里是 parseDouble() 的文档。

<我>
公共静态双 parseDouble(String s)
抛出 NumberFormatException

返回一个新的 double,初始化为指定 String 表示的值,由 Double 类的 valueOf 方法执行。

Convert the string to a double in order to do the multiplication. Double.parseDouble() is a method you could use. I chose to wrap this in a try block in case the string is malformed and an error is thrown (if price is for some reason not a number - ex. 5.67b or 8..34).

String price = dataRecord.get( "PRICE" );
try {
    double p = Double.parseDouble(price);
    double result = p*1.28;
    System.out.println(result);
} catch(NumberFormatException e) { // ERROR HANDLING
    System.out.println("Error parsing string: Price is not a number.");
}

For your reference, here is the documentation for parseDouble().


public static double parseDouble(String s)
throws NumberFormatException

Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.

套路撩心 2024-10-17 16:02:01

浮点数学。使用 BigDecimal 并且您需要设置结果的比例以调用舍入,因为比例很可能会比您想要的更大。

private static final BigDecimal MULT = new BigDecimal("1.28");

BigDecimal price = new BigDecimal(dataRecord.get("PRICE"));
BigDecimal result = price.multiply(MULT).setScale(2, RoundingMode.HALF_EVEN);

Floating point math. Use BigDecimal and you need to set the scale on the result to invoke rounding as most likely the scale will be bigger than what you want.

private static final BigDecimal MULT = new BigDecimal("1.28");

BigDecimal price = new BigDecimal(dataRecord.get("PRICE"));
BigDecimal result = price.multiply(MULT).setScale(2, RoundingMode.HALF_EVEN);
面犯桃花 2024-10-17 16:02:01

简单的方法:执行以下操作
Double.ParseInt("Stringname"),将 reutnr 双精度值
如果您喜欢这个答案,请给予一些信任

Simple way: do the following
Double.ParseInt("Stringname"), will reutnr the double value
PLS give some credit if you like the answer

青朷 2024-10-17 16:02:01

我希望这就是您正在寻找的

String priceAsString = dataRecord.get( "PRICE" );

double priceAsInt = Double.valueOf(priceAsString).doubleValue();
double multipliedPrice = priceAsInt * 1.28;

如果您不需要在乘以值后将其转换为字符串,只需删除最后一行即可。

I hope this is what you are looking for

String priceAsString = dataRecord.get( "PRICE" );

double priceAsInt = Double.valueOf(priceAsString).doubleValue();
double multipliedPrice = priceAsInt * 1.28;

If you don't need to convert it to a String after multiplying the value just remove the last line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文