3 回答
TA贡献1951条经验 获得超3个赞
printf
printf("%.6g", 359.013); // 359.013printf("%.6g", 359.01); // 359.01
printf("%.6g", 3.01357); // 3.01357
sprintf("%.20g")
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
TA贡献1895条经验 获得超3个赞
float f = 1234.56789;printf("%d.%.0f", f, 1000*(f-(int)f));
能量为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
- 3 回答
- 0 关注
- 469 浏览
添加回答
举报