Setting decimal places in C program in currency convertor

I need to know if its possible to set the number of decimal places in the below code and if so how?

Many thanks

Rodney

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <math.h>

int main()  {
int a =2;
int b =4;
int c =6;
int d =8;

int currencyOption;

printf("\n *********************************************");
printf("\n *********************************************");
printf("\n ** Enter the number from the below list    ** ");
printf("\n ** of currency you wish to convert to US$  **");
printf("\n **                                         **");
printf("\n **                                         **");
printf("\n **    1 = Australian dollars.              **");
printf("\n **    2 = British Pounds.                  **");
printf("\n **    3 = Japanese yen.                    **");
printf("\n **    4 = Vietnams dollars.                **");
printf("\n **                                         **");
printf("\n **    Enter Currency number:               **");
printf("\n **                                         **");
printf("\n *********************************************");
printf("\n *********************************************");
printf("\n");

scanf("%d", &currencyOption);
printf("\n");

float xrate;
if(currencyOption == 1) {
	xrate=1.078;
       printf("You want to convert Australian dollars to US$ \n"); }
if(currencyOption == 2) {
	xrate=0.656;
       printf("you want to convert British pounds to US$ \n"); }
if(currencyOption == 3) {
	xrate=98.309;
       printf("you want to convert Japanese yen to US$ \n"); }
if(currencyOption == 4){
	xrate=21025.0;
       printf("you want to convert vietnames dollars to US$ \n"); }

printf("\n");
printf("\n **************************************************");
printf("\n **************************************************");
printf("\n **                                              **");
printf("\n **  Enter the amount you wish to convert to US$ **");
printf("\n **                                              **");
printf("\n **************************************************");
printf("\n **************************************************");
printf("\n");


	float currencyvalue;
printf("\n");

	scanf("%f",&currencyvalue);
	int i=1;
	float result[2];

			int mult = i* 1;
	result[i] = currencyvalue * xrate * mult;
	printf("\n");
	printf("\n");
	printf(" %f To Be Converted\n\n Exchange rate of %f \n\n US$%f will be recieved\n--------------------\n", currencyvalue, xrate, result[i]);	
	printf("\n");

		
	
return 0; }
You can control printf by specifying something like "%7.3f" - this will give you three decimal places, and the number will be filled with spaces on the right side to make it 7 positions in total.
I have tried this and nothing happened. So I am possibly putting the "%7.3f" in the wrong position. Where exactly in the code do you put it.
Please try this code:

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main()
{
	float x = 12.2345;
	printf("%10.3f \n", x);
	return 0;
}
 


Yes you can use double or float instead of int
Topic archived. No new replies allowed.