简体   繁体   English

精确打印双精度

[英]Print a double with its precision

I've got a third party library that read values from laboratory scales. 我有一个第三方库,可以从实验室规模读取值。 This library interface many scale models, each one with its own precision. 该库接口了许多比例模型,每个模型都有自己的精度。 What I need to do in my C++ application (that uses that library) is to read the weight value from the scale (double format) and print it, taking into account the scale precision (so giving the user, reading that value, the information about scale precision). 在我的C ++应用程序(使用该库)中,我需要做的是从秤(双格式)读取重量值并打印出来,同时考虑到秤的精度(因此,为用户提供读取该值的信息)关于比例精度)。

So, connecting a scale with 2 decimals precision, the output should be for example: 23.45 g Instead, connecting a scale with 4 decimals precision, the output should be for example: 23.4567 g 因此,以2位小数精度连接刻度,输出应为:23.45 g例如,以4位小数精度连接刻度,输出应为:23.4567 g

The fact is I don't know from the library the scale precision. 事实是我不知道图书馆的规模精度。 The function looks like the following: 该函数如下所示:

double value = scale.Weight();

If I just print the double value, the output could be in the form of: 如果仅打印double值,则输出形式可能是:

1.345999999999999

instead of: 代替:

1.346

Is there a way to understand the double precision so that the output shows the weight with the scale precision? 有没有办法理解双精度,以便输出显示带有秤精度的重量?

EDIT: scale precision goes from 0 to 6 decimals. 编辑:比例精度从0到6位小数。

No. This information should be inside scale class as double type has "fixed" precision and you cannot change it. 不能。此信息应在scale类中,因为double类型具有“固定”精度,您不能更改它。 Also type precision and printed precision are two different things. 文字精度和印刷精度也是两件事。 You can use type that has infinite precision but show always 2 digits after dot etc. If scale do not have precision information you could do a helper class and hard code precision inside it then correlate it with some scale property or type. 您可以使用具有无限精度的类型,但在点等之后始终显示2位数字。如果scale没有精度信息,则可以在其中进行辅助类和硬代码精度的操作,然后将其与某些scale属性或类型相关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM