Java Double 到 String 的无格式转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1631091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:00:53  来源:igfitidea点击:

Java Double to String conversion without formatting

javastringformatdouble

提问by Mizipzor

I have the number 654987. Its an ID in a database. I want to convert it to a string. The regular Double.ToString(value) makes it into scientific form, 6.54987E5. Something I dont want.

我的号码是 654987。它是数据库中的 ID。我想将其转换为字符串。常规 Double.ToString(value) 使其成为科学形式,6.54987E5。我不想要的东西。

Other formatting functions Ive found checks the current locale and adds appropriate thousand separators and such. Since its an ID, I cant accept any formatting at all.

我发现的其他格式化功能会检查当前语言环境并添加适当的千位分隔符等。由于它是一个 ID,我根本无法接受任何格式。

How to do it?

怎么做?

[Edit] To clarify: Im working on a special database that treats all numeric columns as doubles. Double is the only (numeric) type I can retrieve from the database.

[编辑] 澄清:我正在研究一个特殊的数据库,该数据库将所有数字列视为双精度。Double 是我可以从数据库中检索的唯一(数字)类型。

采纳答案by alphazero

Use Long:

使用长:

long id = 654987;
String str = Long.toString(id);

回答by Ned Batchelder

If it's an integer id in the database, use an Integer instead. Then it will format as an integer.

如果它是数据库中的整数 id,请改用 Integer。然后它将格式化为整数。

回答by Eemeli Kantola

What about:

关于什么:

Long.toString(value)

or

或者

new String(value)

回答by rsp

How about String.valueOf((long)value);

怎么样 String.valueOf((long)value);

回答by Joachim Sauer

Use a fixed NumberFormat(specifically a DecimalFormat):

使用固定的NumberFormat(特别是 a DecimalFormat):

double value = getValue();
String str = new DecimalFormat("#").format(value);

alternatively simply cast to int(or longif the range of values it too big):

或者简单地转换为int(或者long如果值的范围太大):

String str = String.valueOf((long) value);

But then again: why do you have an integer value (i.e. a "whole" number) in a doublevariable in the first place?

但话又说回来:为什么你double首先在变量中有一个整数值(即“整数”)?

回答by DJClayworth

If what you are storing is an ID (i.e. something used only to identify another entity, whose actual numeric value has no significance) then you shouldn't be using Double to store it. Precision will almost certainly screw you.

如果您存储的是 ID(即仅用于标识另一个实体,其实际数值没有意义的东西),那么您不应该使用 Double 来存储它。精度几乎肯定会让你失望。

If your database doesn't allow integer values then you should stored IDs as strings. If necessary make the string the string representation of the integer you want to use. With appropriate use of leading zeros you can make the alphabetic order of the string the same as the numeric order of the ints.

如果您的数据库不允许整数值,那么您应该将 ID 存储为字符串。如有必要,使字符串成为您要使用的整数的字符串表示形式。通过适当使用前导零,您可以使字符串的字母顺序与整数的数字顺序相同。

That should get you round the issue.

这应该能让你解决这个问题。

回答by amit.bhayani

    double d = 56789;
    String s = d+"";

回答by Bobs

Also you can use

你也可以使用

double value = getValue();

NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);

String strVal = f.format(value);

回答by Yotam Abramson

What about Long.toString((long)value) ?

Long.toString((long)value) 呢?