Java - 如何从整数(金钱)中获取双精度数

发布于 2024-12-09 14:24:39 字数 328 浏览 0 评论 0原文

我得到:

int number = 1255; -> //It could also be 125(1€25Cent) or 10(10Cent) or 5(5Cent)

public double toMoney(int number)
{
...
}

作为返回,我想要双数:12.55或者如果输入:10则:00.10

我知道我可以用 Modulo 做这样的事情:

1255 % 100 .. 得到 55.. 但是如何做到 12 最后,如何 将其形成为双精度?

I got:

int number = 1255; -> //It could also be 125(1€25Cent) or 10(10Cent) or 5(5Cent)

public double toMoney(int number)
{
...
}

as return, I want the double number: 12.55 or if input: 10 then: 00.10

I know that I can do with Modulo something like this:

1255 % 100.. to get 55.. But how to do it for 12 and at the end, how to
form it as a double?

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

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

发布评论

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

评论(6

何止钟意 2024-12-16 14:24:39

该方法不应该存在,因为它无法给出正确的结果。

因为在内部,double 是一种无法准确表示 0.1、0.2 等数字的格式(二进制浮点)或 0.3根本。请阅读浮点指南了解更多信息。

如果需要小数,则输出格式应为 BigDecimalString

That method should not exist, because it cannot give correct results.

Because internally, double is a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all. Read the Floating-Point Guide for more information.

If you need decimals, your output format should be BigDecimal or String.

后来的我们 2024-12-16 14:24:39

打印金额的方法是使用 NumberFormat 类!

查看此示例:

NumberFormat format = NumberFormat.getCurrencyInstance(Locale.FRANCE);
format.setCurrency(Currency.getInstance("EUR"));
System.out.println( format.format(13234.34) );

打印此输出:

13 234,34 €

您可以尝试不同的区域设置和货币代码。请参阅文档:http://download.oracle。 com/javase/1.4.2/docs/api/java/text/NumberFormat.html

The way printing money amounts should be done is by using NumberFormat class!

Check out this example:

NumberFormat format = NumberFormat.getCurrencyInstance(Locale.FRANCE);
format.setCurrency(Currency.getInstance("EUR"));
System.out.println( format.format(13234.34) );

Which print this output:

13 234,34 €

You can try different locales and currency codes. See docs: http://download.oracle.com/javase/1.4.2/docs/api/java/text/NumberFormat.html.

凉城 2024-12-16 14:24:39

如果我正确理解您的问题,您可能会尝试这样做:

return (double)number / 100.;

尽管有一句警告: 由于舍入,您不应该使用浮点货币 -如果

您只想以货币格式打印数字,这里有一个 100% 安全的方法:

System.out.print((number / 100) + ".");
int cents = number % 100;
if (cents < 10)
    System.out.print("0");
System.out.println(cents);

这可能可以更好地简化...当然您可以使用 BigDecimal,但在我看来,这打破了蚂蚁与一个大锤。

If I'm understanding your question correctly, you're probably trying to just do this:

return (double)number / 100.;

Though a word of warning: You shouldn't be using floating-point for money due to round-off issues.

If you just want to print the number in money format, here's a 100% safe method:

System.out.print((number / 100) + ".");
int cents = number % 100;
if (cents < 10)
    System.out.print("0");
System.out.println(cents);

This can probably be simplified a lot better... Of course you can go with BigDecimal, but IMO that's smashing an ant with a sledgehammer.

画尸师 2024-12-16 14:24:39
public double toMoney(int number)
{
  return number / 100.0;
}

如果您处理的是货币金额,请记住,将数字转换为双精度时可能会出现一些精度损失。

public double toMoney(int number)
{
  return number / 100.0;
}

If you're dealing with monetary amounts, do bear in mind that some loss of precision can occur when you convert the number to a double.

尽揽少女心 2024-12-16 14:24:39
public double toMoney(int number)
{
return number / 100.0
}

诀窍是使用 100.0 而不是 100 来强制 java 使用双除法。

public double toMoney(int number)
{
return number / 100.0
}

The trick is to use 100.0 rather than 100 to force java to use double division.

十六岁半 2024-12-16 14:24:39
public double toMoney(int number)
{
    return number / 100.0;
}

PS 你不应该使用 double 来表示货币价值,这是个坏主意。

public double toMoney(int number)
{
    return number / 100.0;
}

P.S. You shouldn't use double for money values, bad idea.

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