英文:
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
我猜你手头有一个以字符串格式表示的十进制数,想要将它转换为十进制格式,然后再进行反向转换。你试过以下的方法了吗:
String decimalAsString = "100.99";
double decimalAsDouble = Double.parseDouble(decimalAsString);
System.out.println("Decimal as Double: " + decimalAsDouble);
String decimalToStringAgain = Double.toString(decimalAsDouble);
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:
String decimalAsString = "100.99";
double decimalAsDouble = Double.parseDouble(decimalAsString);
System.out.println("Decimal as Double: " + decimalAsDouble);
String decimalToStringAgain = Double.toString(decimalAsDouble);
System.out.println("Decimal as String: " + decimalToStringAgain);
答案2
得分: 0
不确定您是否指的是将字符串的数据类型更改为双精度,例如,如果您有一个字符串"6.23",并希望将其转换为双精度"6.23"。您可以使用parseDouble()方法。
public class StringToDecimal {
public static void main(String[] args) {
String stringNum = "6.23";
double decimalNum;
decimalNum = Double.parseDouble(stringNum);
System.out.println(decimalNum);
}
}
英文:
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.
public class StringToDecimal {
public static void main(String[] args) {
String stringNum = "6.23";
double decimalNum;
decimalNum = Double.parseDouble(stringNum);
System.out.println(decimalNum);
}
}
答案3
得分: 0
好的,以下是翻译好的部分:
好的,现在有其他答案了(尽管问题中没有展示努力),感觉需要提供一些备选方案:BigDecimal
请参阅以下示例:
public static void main(String[] args) {
// 将小数提供为文本
String decimalText = "512.56";
// 直接在BigDecimal的构造函数中使用它
BigDecimal bigDecimal = new BigDecimal(decimalText);
// 并获取该BigDecimal的double值
double decimal = bigDecimal.doubleValue();
// 然后输出对象或基元的一些表示形式
System.out.println(
String.format("Text: %s, BigDecimal.toPlainString(): %s, double: %f",
decimalText, bigDecimal.toPlainString(), decimal)
);
}
其输出为
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:
public static void main(String[] args) {
// provide a decimal as text
String decimalText = "512.56";
// use it directly in the constructor of a BigDecimal
BigDecimal bigDecimal = new BigDecimal(decimalText);
// and receive the double value of that BigDecimal
double decimal = bigDecimal.doubleValue();
// then output some representations of the objects resp. primitives
System.out.println(
String.format("Text: %s, BigDecimal.toPlainString(): %s, double: %f",
decimalText, bigDecimal.toPlainString(), decimal)
);
}
which outputs
Text: 512.56, BigDecimal.toPlainString(): 512.56, double: 512,560000
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论