将大十进制转换为不带指数格式的双精度 [英] Converting big decimal to double without exponential format

查看:103
本文介绍了将大十进制转换为不带指数格式的双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对具有十进制精度的BigDecimal对象进行计算.

I'm doing calculations on High decimal precision BigDecimal objects.

我正在使用第三方库,该库需要将参数加倍.当我尝试将其转换为两倍时,我得到的是指数格式的值,而不是小数.

I'm using a third party library that requires the parameter in double. When I try to convert this to double i get the values in exponential format instead of decimals .

BigDecimal 0.000035000000000
Double 3.5E-5

BigDecimal bd; 
BigDecimal(0.0000349999999995631583260546904057264328002929687500);
bd= bd.setScale(15,BigDecimal.ROUND_CEILING);

Log.i("bd","bd "+bd.toPlainString());
Log.i("bd","double"+bd.doubleValue());

我发现了将3.5E-5值转换为正确的十进制小数点(但仅以String格式)的各种方法.而我需要将值加倍

I have found various ways of converting this 3.5E-5 value as correct decimal in points but in String format only . Whereas I need the value in double

能够在String对象中获得指数而不是两倍

  DecimalFormat df = new DecimalFormat("#");
        df.setMaximumFractionDigits(16);
       String test=df.format(bd,doubleValue());
        System.out.println("op2 "+test);
        Double test1=Double.parseDouble(test);
        System.out.println("op2 "+test1);

输出op2 .000035 op2 3.5E-5

OUTPUT op2 .000035 op2 3.5E-5

自从几天以来,我一直在努力寻找解决方案,因为我无法控制要求该值必须为两倍的第三方库.

I'm struggling since since days to find a solution as I have no control over 3rd party library that requires the value to be in double

引用链接

格式化BigDecimal且不带有科学注释,且具有完全的精确性

将Java中的指数值转换为数字格式

在Java中从指数形式转换为十进制

如何解决Java四舍五入问题

不带E的Java BigDecimal

如何避免双重使用科学计数法?

我的问题与这些链接不同,我不希望没有指数形式的double作为字符串,而是只希望使用double格式.

My question is different then these links, I do not want the double as string without exponential instead I want it in double format only.

编辑

如果传递的双精度值是指数格式,则基于NDK的库将进一步对该对象进行计算,这将导致更多的小数点和更大的指数值.因此,我想传递一个不带E的简单double

If the double value passed is in exponential format, the NDK based libary will further do calculations on this object, which will result inmore decimal points and larger Exponential value. So, I want to pass a simple double without E

推荐答案

您似乎将double(一个64位的值,其中不同的位具有不同的含义)及其字符串表示形式混淆了.

You seem to be confusing a double, a 64 bit value where different bits have a different meaning, with its string representation.

让我们取值0.015625.在内部,双精度表示为64位(我现在不知道是哪种位组合,但是无论如何都没有关系).

Let's take a value of 0.015625. Internally, the double is represented as 64 bits (I don't know right now which combination of bits, but that is irrelevant anyway).

但是字符串表示形式取决于您所使用的库的格式设置.注意,表示0.0156251.5625E-2表示完全相同的double值.双精度型不存储为字符串,不包含.E,仅包含多个位数.这些位的组合形成其实际值,并且无论您如何将其表示为字符串,它都是相同的.如果您使用其他格式设置,则结果也可能为1.56E-2.这仅意味着将double的内部位组合转换为字符串的代码将进行四舍五入.

But the string representation depends on the format settings of the library you are using. Note that the representations 0.015625 and 1.5625E-2 represent the exact same double value. The double is not stored as a string, it does not contain a . or an E, just a number of bits. The combination of these bits form its real value, and it is the same, no matter how you represent it as a string. If you have a different format setting, the result can just as well be 1.56E-2. This just means that the code that converts the internal bit combination of the double to a string does some rounding.

因此,要从BigDecimal获得double,只需使用bd.doubleValue().不需要使用中间的字符串表示形式,这样做甚至是有害的,因为如果字符串表示形式执行一些舍入操作,您将无法得到BigDecimal中值的最佳近似值.

So, to obtain a double from a BigDecimal, simply use bd.doubleValue(). There is no need to use an intermediate string representation, and it can even be detrimental to do so, because if the string representation performs some rounding, you don't get the best approximation of the value in the BigDecimal.

同样,bd.doubleValue()是执行此操作的唯一正确方法.这不会返回特定格式,而只是返回最接近BigDecimal中的值的double.

So again, bd.doubleValue() is the only correct way to do this. This does not return a specific format, it simply returns the double that is closest to the value in the BigDecimal.

同样,不要将double类型与它如何表示为字符串混淆.这些是不同的东西. doubledoubledouble,并且不存储为字符串,指数或纯或四舍五入的字符串.

And again, do not confuse the double type with how it is represented as a string. These are different things. A double is a double is a double, and that is not stored as a string, exponential or plain or rounded or whatever.

实际上,BigDecimal.toString()BigDecimal.toPlainString()还会返回同一BigDecimal的不同字符串表示形式. AFAIK,甚至还有第三个字符串转换函数toEngineeringString(),它为您提供了同一BigDecimal的另一个字符串表示形式.这与double相似:具有不同参数的不同输出例程将针对相同的值返回不同的字符串.

Actually, BigDecimal.toString() and BigDecimal.toPlainString() also return different string representations of the same BigDecimal. AFAIK, there is even a third string conversion function, toEngineeringString(), which gives you yet another string representation of the same BigDecimal. This is similar for double: different output routines with different arguments return different strings for the exact same value.

这篇关于将大十进制转换为不带指数格式的双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆