将十进制转换为文本,然后在Java中再转回十进制。

huangapple go评论98阅读模式
英文:

Convert decimal to text & back again in Java

问题

我尝试将文本转换为十进制但失败了,我搜索过但没有找到关于Java的结果,文本可以转换为二进制然后再转换为十进制,但我想要直接转换,然后再转换回来,请帮助我。

英文:

I have been trying to convert text to decimal but failed, I searched but dint get results for java, text can be converted to binary then to decimal, but I want direct conversion and back again, please help me.

答案1

得分: 0

我猜你手头有一个以字符串格式表示的十进制数,想要将它转换为十进制格式,然后再进行反向转换。你试过以下的方法了吗:

  1. String decimalAsString = "100.99";
  2. double decimalAsDouble = Double.parseDouble(decimalAsString);
  3. System.out.println("Decimal as Double: " + decimalAsDouble);
  4. String decimalToStringAgain = Double.toString(decimalAsDouble);
  5. System.out.println("Decimal as String: " + decimalToStringAgain);
英文:

I guess you have a decimal number in String format and want to convert it to decimal format and back. Did you Try the following:

  1. String decimalAsString = "100.99";
  2. double decimalAsDouble = Double.parseDouble(decimalAsString);
  3. System.out.println("Decimal as Double: " + decimalAsDouble);
  4. String decimalToStringAgain = Double.toString(decimalAsDouble);
  5. System.out.println("Decimal as String: " + decimalToStringAgain);

答案2

得分: 0

不确定您是否指的是将字符串的数据类型更改为双精度,例如,如果您有一个字符串"6.23",并希望将其转换为双精度"6.23"。您可以使用parseDouble()方法。

  1. public class StringToDecimal {
  2. public static void main(String[] args) {
  3. String stringNum = "6.23";
  4. double decimalNum;
  5. decimalNum = Double.parseDouble(stringNum);
  6. System.out.println(decimalNum);
  7. }
  8. }
英文:

Not sure if you mean changing the datatype of a string to double, eg. if you have a string "6.23" and want to convert the datatype of double "6.23". you could use the parseDouble() method.

  1. public class StringToDecimal {
  2. public static void main(String[] args) {
  3. String stringNum = "6.23";
  4. double decimalNum;
  5. decimalNum = Double.parseDouble(stringNum);
  6. System.out.println(decimalNum);
  7. }
  8. }

答案3

得分: 0

好的,以下是翻译好的部分:

好的,现在有其他答案了(尽管问题中没有展示努力),感觉需要提供一些备选方案:BigDecimal

请参阅以下示例:

  1. public static void main(String[] args) {
  2. // 将小数提供为文本
  3. String decimalText = "512.56";
  4. // 直接在BigDecimal的构造函数中使用它
  5. BigDecimal bigDecimal = new BigDecimal(decimalText);
  6. // 并获取该BigDecimal的double值
  7. double decimal = bigDecimal.doubleValue();
  8. // 然后输出对象或基元的一些表示形式
  9. System.out.println(
  10. String.format("Text: %s, BigDecimal.toPlainString(): %s, double: %f",
  11. decimalText, bigDecimal.toPlainString(), decimal)
  12. );
  13. }

其输出为

  1. Text: 512.56, BigDecimal.toPlainString(): 512.56, double: 512.560000
英文:

OK, now that there are other answers (even though no effort was shown in the question), it feels like having to provide some alternative: A BigDecimal

See the following example:

  1. public static void main(String[] args) {
  2. // provide a decimal as text
  3. String decimalText = "512.56";
  4. // use it directly in the constructor of a BigDecimal
  5. BigDecimal bigDecimal = new BigDecimal(decimalText);
  6. // and receive the double value of that BigDecimal
  7. double decimal = bigDecimal.doubleValue();
  8. // then output some representations of the objects resp. primitives
  9. System.out.println(
  10. String.format("Text: %s, BigDecimal.toPlainString(): %s, double: %f",
  11. decimalText, bigDecimal.toPlainString(), decimal)
  12. );
  13. }

which outputs

  1. Text: 512.56, BigDecimal.toPlainString(): 512.56, double: 512,560000

huangapple
  • 本文由 发表于 2020年9月18日 15:38:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63951261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定