饮歌长啸
这不能用正常的printf格式说明符。你能得到的最接近的是:printf("%.6g", 359.013); // 359.013printf("%.6g", 359.01); // 359.01但“6”是共计数值宽度printf("%.6g", 3.01357); // 3.01357打破它。你能,会,可以做是为了sprintf("%.20g")字符串缓冲区中的数字,然后操作该字符串,使N个字符超过小数点。假设您的数字在变量num中,下面的函数将删除第一个之外的所有N小数,然后去掉尾随的零(如果它们都是零,则取小数点)。char str[50];sprintf (str,"%.20g",num); // Make the number.morphNumericString (str, 3);: :void morphNumericString (char *s, int n) {
char *p;
int count;
p = strchr (s,'.'); // Find decimal point, if any.
if (p != NULL) {
count = n; // Adjust for more or less decimals.
while (count >= 0) { // Maximum decimals allowed.
count--;
if (*p == '\0') // If there's less than desired.
break;
p++; // Next character.
}
*p-- = '\0'; // Truncate string.
while (*p == '0') // Remove trailing zeros.
*p-- = '\0';
if (*p == '.') { // If all decimals were zeros, remove ".".
*p = '\0';
}
}}如果您对截断方面不满意(这会导致0.12399进0.123而不是把它四舍五入0.124),您可以实际使用printf..您只需要分析手边的数字,就可以动态地创建宽度,然后使用这些数据将数字转换为字符串:#include <stdio.h>void nDecimals (char *s, double d, int n) {
int sz; double d2;
// Allow for negative.
d2 = (d >= 0) ? d : -d;
sz = (d >= 0) ? 0 : 1;
// Add one for each whole digit (0.xx special case).
if (d2 < 1) sz++;
while (d2 >= 1) { d2 /= 10.0; sz++; }
// Adjust for decimal point and fractionals.
sz += 1 + n;
// Create format string then use it.
sprintf (s, "%*.*f", sz, n, d);}int main (void) {
char str[50];
double num[] = { 40, 359.01335, -359.00999,
359.01, 3.01357, 0.111111111, 1.1223344 };
for (int i = 0; i < sizeof(num)/sizeof(*num); i++) {
nDecimals (str, num[i], 3);
printf ("%30.20f -> %s\n", num[i], str);
}
return 0;}整点nDecimals()在本例中,要正确计算字段宽度,然后根据字段宽度使用格式字符串对数字进行格式化。试驾main()在行动中显示了这一点: 40.00000000000000000000 -> 40.000
359.01335000000000263753 -> 359.013-359.00999000000001615263 -> -359.010
359.00999999999999090505 -> 359.010
3.01357000000000008200 -> 3.014
0.11111111099999999852 -> 0.111
1.12233439999999995429 -> 1.122一旦得到正确的舍入值,就可以再次将其传递给morphNumericString()通过简单的更改来删除尾随零点:nDecimals (str, num[i], 3);转入:nDecimals (str, num[i], 3);morphNumericString (str, 3);(或呼叫)morphNumericString在.的末尾nDecimals但是,在这种情况下,我可能会将这两个函数合并为一个函数),最后您将得到: 40.00000000000000000000 -> 40
359.01335000000000263753 -> 359.013-359.00999000000001615263 -> -359.01
359.00999999999999090505 -> 359.01
3.01357000000000008200 -> 3.014
0.11111111099999999852 -> 0.111
1.12233439999999995429 -> 1.122
蛊毒传说
我喜欢R.的回答,稍作改动:float f = 1234.56789;printf("%d.%.0f", f, 1000*(f-(int)f));“1000”决定了精确度。能量为0.5四舍五入。编辑好的,这个答案被编辑了几次,几年前我失去了我的想法(最初它并没有满足所有的标准)。下面是一个新版本(填充所有标准并正确处理负数):double f = 1234.05678900;char s[100]; int decimals = 10;sprintf(s,"%.*g", decimals, ((int)(pow(10, decimals)*(fabs(f) - abs((int)f)) +0.5))
/pow(10,decimals));printf("10 decimals: %d%s\n", (int)f, s+1);以及测试用例:#import <stdio.h>#import <stdlib.h>#import <math.h>int main(void){
double f = 1234.05678900;
char s[100];
int decimals;
decimals = 10;
sprintf(s,"%.*g", decimals, ((int)(pow(10, decimals)*(fabs(f) - abs((int)f)) +0.5))/pow(10,decimals));
printf("10 decimals: %d%s\n", (int)f, s+1);
decimals = 3;
sprintf(s,"%.*g", decimals, ((int)(pow(10, decimals)*(fabs(f) - abs((int)f)) +0.5))/pow(10,decimals));
printf(" 3 decimals: %d%s\n", (int)f, s+1);
f = -f;
decimals = 10;
sprintf(s,"%.*g", decimals, ((int)(pow(10, decimals)*(fabs(f) - abs((int)f)) +0.5))/pow(10,decimals));
printf(" negative 10: %d%s\n", (int)f, s+1);
decimals = 3;
sprintf(s,"%.*g", decimals, ((int)(pow(10, decimals)*(fabs(f) - abs((int)f)) +0.5))/pow(10,decimals));
printf(" negative 3: %d%s\n", (int)f, s+1);
decimals = 2;
f = 1.012;
sprintf(s,"%.*g", decimals, ((int)(pow(10, decimals)*(fabs(f) - abs((int)f)) +0.5))/pow(10,decimals));
printf(" additional : %d%s\n", (int)f, s+1);
return 0;}以及测试的输出: 10 decimals: 1234.056789
3 decimals: 1234.057
negative 10: -1234.056789
negative 3: -1234.057
additional : 1.01现在,所有标准都得到满足:零后面的最大小数是固定的。移除尾随零点它在数学上是正确的(对吗?)当第一个小数为零时(现在)也工作。不幸的是,这个答案是两条直线的sprintf不返回字符串。