gpt4 book ai didi

c - 使用变量设置 float 宽度和精度

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:56 24 4
gpt4 key购买 nike

我正在尝试打印具有用户可变宽度和精度的 float 。我遇到的问题是只读取精度字段,宽度被完全忽略。

int width = 4; int precision = 12;
printf("%*.*lf ", width , precision, value);

605.32364879299 5980.79287087619 802.32966093936
5839.78856776635 5355.77379680776 6602.20148319956

本应填充空格以确保小数位对齐,但就好像甚至没有读取宽度变量一样。

最佳答案

width is being completely ignored.

肯定是对字段宽度的误解。这是要打印的最小字符数,包括符号、数字、小数点等。

An optional minimum field width. If the converted value has fewer characters than the field width, it is padded with spaces ... C11dr §7.21.6.1 4

int width = 4; int precision = 12;
double value = 605.32364879299;
printf("%*.*lf\n", width , precision, value);
width = 21;
printf("%*.*lf\n", width , precision, value);
/*
123456789012345678901 */

输出

605.323648792990       // Minimum width =  4, 16 characters in numeric text
605.323648792990 // Minimum width = 21, 5 spaces + 16 characters in numeric text

关于c - 使用变量设置 float 宽度和精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48594330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com