gpt4 book ai didi

Java:如何从某个字符串位置解析 double 并获取其后面的 char 的位置?

转载 作者:行者123 更新时间:2023-12-02 12:12:44 25 4
gpt4 key购买 nike

我想要类似于 C++ 中的 std::stod 的东西。我发现的最接近的是 DecimalFormat 类,但它的一个大问题是它的行为与 Double.parseDouble() 不同。例如,它将 3.14E+2 解析为 3.14 而不是 314。虽然 Double.parseDouble() 对于这两种情况给出了相同的答案,这就是我想要的行为。仅用 E 替换所有 E+ 并不是一个理想的选择(它可能会破坏其他东西)。

所以基本上我想要 Double.parseDouble() 能够设置起始位置并返回结束位置。有没有什么方法可以在 Java 中实现这一点,而无需重新实现整个双解析例程?

最佳答案

扫描仪类非常接近您的要求:

import java.util.*;
import java.lang.reflect.*;

public class ScanFloat {
public static void main(String args[]) throws Exception {
String str = "foo 3.14E+2 xx";
int startPos = 4;
Scanner s = new Scanner(str.substring(4));
s.useLocale(Locale.ENGLISH);
System.out.println(s.nextDouble());
System.out.println("endingPos: " + (startPos + getPos(s)));

}
static int getPos(Scanner s) throws Exception {
Field f = Scanner.class.getDeclaredField("position");
f.setAccessible(true);
return (int) f.get(s);
}
}

输出:

314.0
endingPos: 11

但是,输入“foo 3.14E+2xx”会抛出异常。根据您的用例,它可能会也可能不会。

使用正则表达式可以解决这个问题,而且也不难,因为您只需使用 patterns from the documentation I linked :

import java.util.regex.*;
import java.util.concurrent.atomic.AtomicInteger;

public class RegexFloat {
public static void main(String args[]) {
String testString = "foo 3.14E+2xx";
AtomicInteger parsePos = new AtomicInteger(0);
Double d = parseDouble(testString, parsePos);
System.out.println(d);
System.out.println(parsePos);
}

static Double parseDouble(String str, AtomicInteger parsePos) {
Pattern pattern = Pattern.compile(fpRegex);
Matcher m = pattern.matcher(str.substring(parsePos.get()));

if (m.find()) {
parsePos.set(m.end(1));
return Double.parseDouble(m.group(1));
}
return null;
}

// or Pattern.compile(String.format(".{%d}%s", parsePos.get(), fpRegex));
// if you want to avoid substring above for some reason and
// want to add offset into the pattern


static final String Digits = "(\\p{Digit}+)";
static final String HexDigits = "(\\p{XDigit}+)";
// an exponent is 'e' or 'E' followed by an optionally
// signed decimal integer.
static final String Exp = "[eE][+-]?"+Digits;
static final String fpRegex =
("[\\x00-\\x20]*"+ // Optional leading "whitespace"
"[+-]?(" + // Optional sign character
"NaN|" + // "NaN" string
"Infinity|" + // "Infinity" string

// A decimal floating-point string representing a finite positive
// number without a leading sign has at most five basic pieces:
// Digits . Digits ExponentPart FloatTypeSuffix
//
// Since this method allows integer-only strings as input
// in addition to strings of floating-point literals, the
// two sub-patterns below are simplifications of the grammar
// productions from section 3.10.2 of
// The Java™ Language Specification.

// Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
"((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+

// . Digits ExponentPart_opt FloatTypeSuffix_opt
"(\\.("+Digits+")("+Exp+")?)|"+

// Hexadecimal strings
"((" +
// 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "(\\.)?)|" +

// 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +

")[pP][+-]?" + Digits + "))" +
"[fFdD]?))" +
"[\\x00-\\x20]*");// Optional trailing "whitespace"
}

输出:

314.0
11

关于Java:如何从某个字符串位置解析 double 并获取其后面的 char 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46412901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com