如何将BigDecimal转换为指数形式?

发布于 2025-01-07 11:11:31 字数 129 浏览 3 评论 0原文

如何将 BigDecimal 对象转换为使用指数形式的字符串表示形式?类似于:3.134e67?我查看了 API,发现了 toEngineeringString() 但它没有给我我想要的东西。

How to convert BigDecimal object to a String representation that uses the exponential form? something like: 3.134e67? I looked into the API and I found toEngineeringString() but it does not give me what I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

短叹 2025-01-14 11:11:32

您是否阅读过 NumberFormat 文档,这是来自 DecimalFormat 的:
请参阅:http://docs.oracle.com /javase/1.4.2/docs/api/java/text/DecimalFormat.html

科学记数法

科学记数法中的数字表示为
尾数和10的幂,例如1234可以表示为
1.234 x 10^3。尾数通常在 1.0 <= x < 范围内。 10.0,但不一定是。 DecimalFormat可以指令格式化和解析
科学记数法仅通过模式;目前没有工厂
创建科学记数格式的方法。在一个模式中,
指数字符后紧跟一个或多个数字
字符表示科学计数法。示例:“0.###E0”格式
数字 1234 为“1.234E3”。

  • 指数字符后的数字字符数给出了最小指数位数。没有上限。消极的
    指数的格式使用本地化的减号,而不是前缀
    以及模式的后缀。这允许诸如“0.###E0
    米/秒”。

  • 解释整数位数的最小和最大数量
    一起:

    • 如果最大整数位数大于最小整数位数
      number 且大于 1,它强制指数为
      最大整数位数和最小整数位数
      被解释为 1 的整数。最常见的用法是
      生成工程符号,其中指数是倍数
      三个,例如“##0.#####E0”。使用此模式,数字 12345
      格式为“12.345E3”,123456 格式为“123.456E3”。

    • 否则,最小整数位数通过以下方式实现
      调整指数。示例:0.00123 格式为“00.###E0”
      产生“12.3E-4”。

  • 尾数中的有效位数是尾数的总和
    最小整数和最大小数位,并且不受
    最大整数位数。例如,12345 格式为“##0.##E0”
    是“12.3E3”。要显示所有数字,请将有效数字计数设置为
    零。有效位数不影响解析。

  • 指数模式不得包含分组分隔符。

Have you read the NumberFormat documentation, this is from DecimalFormat:
See: http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html

Scientific Notation

Numbers in scientific notation are expressed as the product of a
mantissa and a power of ten, for example, 1234 can be expressed as
1.234 x 10^3. The mantissa is often in the range 1.0 <= x < 10.0, but it need not be. DecimalFormat can be instructed to format and parse
scientific notation only via a pattern; there is currently no factory
method that creates a scientific notation format. In a pattern, the
exponent character immediately followed by one or more digit
characters indicates scientific notation. Example: "0.###E0" formats
the number 1234 as "1.234E3".

  • The number of digit characters after the exponent character gives the minimum exponent digit count. There is no maximum. Negative
    exponents are formatted using the localized minus sign, not the prefix
    and suffix from the pattern. This allows patterns such as "0.###E0
    m/s".

  • The minimum and maximum number of integer digits are interpreted
    together:

    • If the maximum number of integer digits is greater than their minimum
      number and greater than 1, it forces the exponent to be a multiple of
      the maximum number of integer digits, and the minimum number of
      integer digits to be interpreted as 1. The most common use of this is
      to generate engineering notation, in which the exponent is a multiple
      of three, e.g., "##0.#####E0". Using this pattern, the number 12345
      formats to "12.345E3", and 123456 formats to "123.456E3".

    • Otherwise, the minimum number of integer digits is achieved by
      adjusting the exponent. Example: 0.00123 formatted with "00.###E0"
      yields "12.3E-4".

  • The number of significant digits in the mantissa is the sum of the
    minimum integer and maximum fraction digits, and is unaffected by the
    maximum integer digits. For example, 12345 formatted with "##0.##E0"
    is "12.3E3". To show all digits, set the significant digits count to
    zero. The number of significant digits does not affect parsing.

  • Exponential patterns may not contain grouping separators.

一生独一 2025-01-14 11:11:32

这对你有帮助吗?

BigDecimal bd = new BigDecimal(3.134e67);
String.valueOf(bd.doubleValue())

Does this help you?

BigDecimal bd = new BigDecimal(3.134e67);
String.valueOf(bd.doubleValue())
Bonjour°[大白 2025-01-14 11:11:32

您在寻找这样的东西吗?

YY ^ XX (mod QQ)

int fastMod(int YY, int XX, int QQ){
    int ZZ;                                             //declare variables
    int RR = 1;

    while (XX != 0){                                    //while XX != 0
        ZZ = XX % 2;                                    //mod XX by 2
        XX= XX/2;                                       //divide XX by 2
        if (ZZ == 1)                                    //if ZZ is one
        RR = (RR * YY) % QQ;                            //mod (RR*YY) by QQ
        YY= (YY * YY) % QQ;                             //mod (YY*YY) by QQ     
    }
    return RR;                                          //return int
}

当您开始使用 GIANT 数字时,您将需要使用模运算。
当像 RSA 算法那样生成大素数时,这变得特别有用。
这里介绍了模运算的基础知识:
http://www.brainjammer.com/math/modular-arithmetic/

Are you looking for something like this?

YY ^ XX (mod QQ)

int fastMod(int YY, int XX, int QQ){
    int ZZ;                                             //declare variables
    int RR = 1;

    while (XX != 0){                                    //while XX != 0
        ZZ = XX % 2;                                    //mod XX by 2
        XX= XX/2;                                       //divide XX by 2
        if (ZZ == 1)                                    //if ZZ is one
        RR = (RR * YY) % QQ;                            //mod (RR*YY) by QQ
        YY= (YY * YY) % QQ;                             //mod (YY*YY) by QQ     
    }
    return RR;                                          //return int
}

When you start using GIANT numbers you will need to use modular arithmetic.
This becomes especially useful when generating large primes like in RSA algorithm.
The basics of modular arithmetic covered here:
http://www.brainjammer.com/math/modular-arithmetic/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文