我編碼類的簡單程序。我已經完成了,不用擔心,我不會要求任何人做我的功課。讓我用一個例子來解釋我想要的。表現出一定的小數位ç
我的程序要求的比特量,並將其轉換到MB,kb和字節。因此,如果輸出I輸入1位是:
1 in megabytes is: 0.000000119209290
1 in kilobytes is: 0.000122070312500
1 in bytes is: 0.125000000000000
1 in bits is: 1
所以,我的問題是,只是一個審美的一個:我怎麼能不顯示小數位是不必要的?例如,在字節,我想只有打印0.125的,而不是15位小數,這是不是漂亮的。
的源代碼是:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
unsigned long long int bits;
printf("Input a quantity of bits: \n");
scanf("%lld", &bits);
/*
* 1 byte = 8 bits.
* 1 kilobyte = 1024 bytes.
* 1 megabyte = 1024 kilobytes.
*/
long double by = ((double) bits)/((double) 8);
long double kb = ((double) by)/((double) 1024);
long double mb = ((double) kb)/((double) 1024);
printf("%lld in megabytes is: %.15Lf\n", bits, mb);
printf("%lld in kilobytes is: %.15Lf\n", bits, kb);
printf("%lld in bytes is: %.15Lf\n", bits, by);
printf("%lld in bits is: %lld\n", bits, bits);
return(0);
}
PS:我知道我指定的15位小數在printf
,我赤身只是想這是最好的辦法,我輸出的值。
預先感謝您!
http://stackoverflow.com/questions/277772/avoid-trailing-zeroes-in-printf 這可能會幫助你。 – venki421 2014-09-28 13:53:13
thig是使用%g我得到兆字節作爲指數,我想要一個小數。 – 2014-09-28 13:55:58
您是否閱讀過[printf(3)](http://man7.org/linux/man-pages/man3/printf.3.html) – 2014-09-28 13:56:14