Java—是否可以计算出字符串的十进制格式

Java—是否可以计算出字符串的十进制格式,java,parsing,decimalformat,Java,Parsing,Decimalformat,我试图找出如何通过字符串计算有效位数,从而计算出十进制数,并用相同的有效位数打印结果。这是一个SSCCE: import java.text.DecimalFormat; import java.text.ParseException; public class Test { public static void main(String[] args) { try { DecimalFormat df = new DecimalFormat()

我试图找出如何通过
字符串
计算有效位数,从而计算出十进制数,并用相同的有效位数打印结果。这是一个SSCCE:

import java.text.DecimalFormat;
import java.text.ParseException;

public class Test {

    public static void main(String[] args) {
        try {
            DecimalFormat df = new DecimalFormat();
            String decimal1 = "54.60"; // Decimal is input as a string with a specific number of significant digits.
            double d = df.parse(decimal1).doubleValue();
            d = d * -1; // Multiply the decimal by -1 (this is why we parsed it, so we could do a calculatin).
            System.out.println(df.format(d)); // I need to print this with the same # of significant digits.
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
我知道
DecimalFormat
的作用是:1)告诉程序您希望小数点的显示方式(
format()
),2)告诉程序希望小数点所表示的字符串采用的格式(
parse()
)。但是,有没有一种方法可以从解析的字符串中推断出
DecimalFormat
,然后使用相同的
DecimalFormat
输出一个数字

int sigFigs = decimal1.split("\\.")[1].length();
计算小数点右边字符串的长度可能是实现目标的最简单方法


计算小数点右边字符串的长度可能是实现目标的最简单方法。

通过
string.indexOf('.')找到它。

public int findDecimalPlaces(字符串输入){
int dot=input.indexOf('.');
if(点<0)
返回0;
返回input.length()-dot-1;
}

您还可以通过
setMinimumFractionDigits()
setMaximumFractionDigits()
配置十进制格式/数字格式来设置输出格式,而不必将模式构建为字符串。

通过
string.indexOf('.')
查找它

public int findDecimalPlaces(字符串输入){
int dot=input.indexOf('.');
if(点<0)
返回0;
返回input.length()-dot-1;
}
您还可以通过
setMinimumFractionDigits()
setMaximumFractionDigits()
配置DecimalFormat/NumberFormat来设置输出格式,而不必将模式构建为字符串。

使用:

或简称:

        System.out.println((new BigDecimal(decimal1)).negate());
使用:

或简称:

        System.out.println((new BigDecimal(decimal1)).negate());

我添加了一个代码来设置小数位数,使其与
decimal1

 public static void main(String[] args) {
        try {
            DecimalFormat df = new DecimalFormat();
            String decimal1 = "54.60"; // Decimal is input as a string with a specific number of significant digits.
            //
            int index = decimal1.indexOf(".");
            int prec = -1;
            if (index != -1) {
                prec = decimal1.substring(index, decimal1.length()).length();
            }
            if (prec>0) {
                df.setMaximumFractionDigits(prec);
                df.setMinimumFractionDigits(prec-1);
            }
            //
            double d = df.parse(decimal1).doubleValue();
            d = d * -1; // Multiply the decimal by -1 (this is why we parsed it, so we could do a calculatin).
            System.out.println(df.format(d)); // I need to print this with the same # of significant digits.
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

我添加了一个代码来设置小数位数,使其与
decimal1

 public static void main(String[] args) {
        try {
            DecimalFormat df = new DecimalFormat();
            String decimal1 = "54.60"; // Decimal is input as a string with a specific number of significant digits.
            //
            int index = decimal1.indexOf(".");
            int prec = -1;
            if (index != -1) {
                prec = decimal1.substring(index, decimal1.length()).length();
            }
            if (prec>0) {
                df.setMaximumFractionDigits(prec);
                df.setMinimumFractionDigits(prec-1);
            }
            //
            double d = df.parse(decimal1).doubleValue();
            d = d * -1; // Multiply the decimal by -1 (this is why we parsed it, so we could do a calculatin).
            System.out.println(df.format(d)); // I need to print this with the same # of significant digits.
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

如果需要小数点,则不能在第一位使用浮点,因为FP没有小数点:FP有二进制位。使用
BigDecimal,
并直接从
字符串构造它。
我完全不明白为什么需要
DecimalFormat
对象。

如果需要小数,就不能在第一位使用浮点,因为FP没有它们:FP有二进制位。使用
BigDecimal,
并直接从
字符串构造它。
我完全不明白为什么需要
DecimalFormat
对象。

可以使用正则表达式将数字字符串转换为格式字符串:

String format = num.replaceAll("^\\d*", "#").replaceAll("\\d", "0");
例如“123.45”->“#0.00”和“123”->“#”

然后将结果用作十进制格式的模式


它不仅可以工作,而且只有一行。

您可以使用正则表达式将数字字符串转换为格式字符串:

String format = num.replaceAll("^\\d*", "#").replaceAll("\\d", "0");
例如“123.45”->“#0.00”和“123”->“#”

然后将结果用作十进制格式的模式


它不仅可以工作,而且只有一行。

这是不正确的--
split()
需要一个正则表达式,所以
匹配任何东西。如果这是固定的,它仍然会失败时,没有小数点。老实说,我不太喜欢PHP式的使用
split()
的方法。这是不正确的--
split()
需要一个正则表达式,所以
匹配任何东西。如果这是固定的,它仍然会失败时,没有小数点。老实说,我不是一个PHP式的使用
split()。它经过测试,输出正确。您需要遍历DecimalFormat的javadoc。@EJP首先运行代码。它经过测试,输出正确。您需要检查DecimalFormat的javadoc。这是一个很好的解决方案。最有用、最准确、最简单的。更不用说
negate()
保持原来的比例,似乎有人在构建
BigDecimal
heh时想到了这个问题。这是一个很好的解决方案。最有用、最准确、最简单的。更不用说
negate()
保持原来的比例,似乎有人在构建
BigDecimal
heh时想到了这个问题。