I'm coding a simple program for class. I've completed it, don't worry, I'm not asking for anyone to do my homework. Let me explain what I want with an example.
My program asks for an amount of bits and converts it to mb, kb and bytes. So, the output if I input 1 bit is:
1 in megabytes is: 0.000000119209290
1 in kilobytes is: 0.000122070312500
1 in bytes is: 0.125000000000000
1 in bits is: 1
So, my question is just an aesthetic one: how could I not show the decimal places that are unnecessary? For example, in the bytes, I would like to only print 0.125, instead of 15 decimals, which isn't that pretty at all.
The source code is:
#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: I know I specified 15 decimal places in the printf
, I wast just trying which was the best way for me to output the values.
Thank you in advance!