在字符串或int之间添加点以创建双JAVA
我需要一些东西来把这样的东西:2993929322变成299392.99322自动,因为它必须做很多。 我发现了格式之类的东西,但它似乎只能在数字前面添加东西,但这必须发生在第六个数字之后 最好是从int直接转换成double。但我甚至还没有找到一种方法从一根绳子上做这件事 所以,如果你们中有人知道这是否可能,请帮助我做到这一点。 它必须是java。如果你也知道任何可以帮助我发布的文档在字符串或int之间添加点以创建双JAVA,java,regex,string,Java,Regex,String,我需要一些东西来把这样的东西:2993929322变成299392.99322自动,因为它必须做很多。 我发现了格式之类的东西,但它似乎只能在数字前面添加东西,但这必须发生在第六个数字之后 最好是从int直接转换成double。但我甚至还没有找到一种方法从一根绳子上做这件事 所以,如果你们中有人知道这是否可能,请帮助我做到这一点。 它必须是java。如果你也知道任何可以帮助我发布的文档 提前感谢如果您使用的是字符串,那么您可以使用正则表达式(虽然效率不高,但清晰易读:p): O/p: 如果您使用
提前感谢如果您使用的是字符串,那么您可以使用正则表达式(虽然效率不高,但清晰易读:p): O/p:
如果您使用的是字符串,那么就可以使用正则表达式(虽然效率不高,但清晰易读:p): O/p: 代码:
public static void main(String args[]) {
int intInput = 123456789; // I am using 123456789 because you want to
// input an integer, and "29939299322" is
// greater than the maximum value for an
// integer: "2147483647"
String input = intInput + ""; // Converts the input into a String
String output = ""; // This is the output String, it will later be
// converted into a double
for (int i = 0; i < input.length(); i++) { // Loops through the input
if (i == input.length() - 5) { // If the index is at the certain
// point in the String...
output += "."; // Adds "."
}
output += input.charAt(i) + ""; // Adds the character into the
// output String after converting it
// into a String
}
double outputDouble = Double.parseDouble(output); // Parses the output
// into a Double
System.out.println(outputDouble); // Outputs Double
}
1234.56789
代码:
public static void main(String args[]) {
int intInput = 123456789; // I am using 123456789 because you want to
// input an integer, and "29939299322" is
// greater than the maximum value for an
// integer: "2147483647"
String input = intInput + ""; // Converts the input into a String
String output = ""; // This is the output String, it will later be
// converted into a double
for (int i = 0; i < input.length(); i++) { // Loops through the input
if (i == input.length() - 5) { // If the index is at the certain
// point in the String...
output += "."; // Adds "."
}
output += input.charAt(i) + ""; // Adds the character into the
// output String after converting it
// into a String
}
double outputDouble = Double.parseDouble(output); // Parses the output
// into a Double
System.out.println(outputDouble); // Outputs Double
}
1234.56789
这必须发生在第六个数字之后。
所以我把123456789转换成123456.789?这应该很容易:解析数字,除以Math.pow(10.0,长度-6)
,然后重新格式化为字符串。或者在索引6处插入一个点。您能更精确一点吗?-是否要在字符串中的6个数字后添加一个点?使用substring谢谢你的快速回复,substring的作用与下面的答案一样,谢谢你的快速回复,对于我来说,忽略这样一个简单的解决方案肯定是一个错误的想法。这必须发生在第六个数字之后。
所以我把123456789转换成123456.789?这应该很容易:解析数字,除以Math.pow(10.0,长度-6)
,然后重新格式化为字符串。或者在索引6处插入一个点。您能更精确一点吗?-是否要在字符串中的6个数字后添加一个点?使用substring谢谢你的快速回复,substring的作用与下面的答案一样,谢谢你的快速回复,我一定是错误的思维方式忽略了这么简单的解决方案。谢谢你的回复,它工作得很好,谢谢你。我将把答案标为asolution@ricardospek-Ywc:)谢谢你的回复,效果不错,谢谢。我将把答案标为asolution@ricardospek-Ywc:)