如何使用“%f”以正确的精度将一个double值填充到一个字符串中 [英] How to use "%f" to populate a double value into a string with the right precision

查看:254
本文介绍了如何使用“%f”以正确的精度将一个double值填充到一个字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 sprintf 来填充一个带有double值的字符串,如下所示:

  sprintf(S,%f,val); 

但精确度被截断到小数点后六位。我需要大约10个小数位的精度。



如何才能实现?

解决方案宽度应该包含小数点。

%[width]。[precision]

>%8.2表示8个字符宽;点前5位,后2位。一个字符是保留的点。



<5> 1 + 2 = 8


I am trying to populate a string with a double value using a sprintf like this:

sprintf(S, "%f", val);

But the precision is being cut off to six decimal places. I need about 10 decimal places for the precision.

How can that be achieved?

解决方案

%[width].[precision]

Width should include the decimal point.

%8.2 means 8 characters wide; 5 digits before the point and 2 after. One character is reserved for the point.

5 + 1 + 2 = 8

这篇关于如何使用“%f”以正确的精度将一个double值填充到一个字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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