English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm writing a C (not C++) program where I want to convert money from one currency to another and I need to round US dollars to the nearest cent. I was thinking about storing the number into an array of type char, but I do not know how to do this.

The user will enter a double type number, then a calculation will be performed on this number to convert it to the other country's currency, then this converted number needs to be rounded to 2 decimal places. So how the hell do I round a number after doing a calculation on the number that the user inputs? How can I store this unknown converted number into a char array of unknown size? How can I round this number to 2 decimal places, which then I will print out? Please help me, as I am a beginner in C programming and do not know much about C. Once again, this program is in C and not C++.

2007-02-05 07:27:17 · 2 answers · asked by anonms420 1 in Computers & Internet Programming & Design

2 answers

I would suggest you work with float or double numbers, to allow odd exchange rates.
Then, when printing, the printf() function allows you to format the output. Limit the floating point number to 2 decimal places, to show cents.

double val = 35.945;
printf("Value = %7.2f" , Val);

2007-02-05 11:13:03 · answer #1 · answered by Alan 6 · 0 0

you're dealing with an outstanding sort of difficulty to try this! chars are already integers. (with a limited variety -- they're one byte each [ by definition]) you're able to do math operations on them only like integers considering that they're integers. only becdause they are able to hold the ASCII code for a letter/digit does not lead them to non-integers. #incorporate int significant(void){ char charray[4]={ 3, 13 ,5 ,12}; int num1,num2; num1=charray[0]+charray[a million]; num2=charray[0]*charray[a million]; cout << num1 << " "<< num2; charray[0]='a million'; charray[a million]='3'; num1=(charray[0]-'0') +(charray[a million]-'0'); // to transform a decimal character digit to its integer fee only subtract '0' // that works 'reason the characters 0 by 9 are so as and next to one yet another. cout << " " << num1; // in case you replica them to an int cout will print the fee (no casting needed) // in case you go away them as chars cout will print the character glyph linked with the ASCII code fee interior the char. return 0; }

2016-09-28 11:18:20 · answer #2 · answered by ? 3 · 0 0

fedest.com, questions and answers