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是我可以从数据库中检索的唯一(数字)类型。
9 个解决方案
#1
20
Use Long:
long id = 654987;
String str = Long.toString(id);
#2
32
Use a fixed NumberFormat
(specifically a DecimalFormat
):
使用固定的NumberFormat(特别是DecimalFormat):
double value = getValue();
String str = new DecimalFormat("#").format(value);
alternatively simply cast to int
(or long
if 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 double
variable in the first place?
但话说回来:为什么你首先在双变量中有一个整数值(即“整数”)?
#3
6
If it's an integer id in the database, use an Integer instead. Then it will format as an integer.
如果它是数据库中的整数id,请改用Integer。然后它将格式化为整数。
#4
6
How about String.valueOf((long)value);
String.valueOf((long)value);
#5
2
What about:
Long.toString(value)
or
new String(value)
#6
2
Also you can use
你也可以使用
double value = getValue();
NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
String strVal = f.format(value);
#7
1
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.
这应该让你解决问题。
#8
0
What about Long.toString((long)value) ?
那么Long.toString((长)值呢?)
#9
-3
double d = 56789;
String s = d+"";
#1
20
Use Long:
long id = 654987;
String str = Long.toString(id);
#2
32
Use a fixed NumberFormat
(specifically a DecimalFormat
):
使用固定的NumberFormat(特别是DecimalFormat):
double value = getValue();
String str = new DecimalFormat("#").format(value);
alternatively simply cast to int
(or long
if 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 double
variable in the first place?
但话说回来:为什么你首先在双变量中有一个整数值(即“整数”)?
#3
6
If it's an integer id in the database, use an Integer instead. Then it will format as an integer.
如果它是数据库中的整数id,请改用Integer。然后它将格式化为整数。
#4
6
How about String.valueOf((long)value);
String.valueOf((long)value);
#5
2
What about:
Long.toString(value)
or
new String(value)
#6
2
Also you can use
你也可以使用
double value = getValue();
NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
String strVal = f.format(value);
#7
1
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.
这应该让你解决问题。
#8
0
What about Long.toString((long)value) ?
那么Long.toString((长)值呢?)
#9
-3
double d = 56789;
String s = d+"";