Sunday, July 12, 2009

How do I store a number that the user will input into a char array in C? (please see details below)?

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++.

How do I store a number that the user will input into a char array in C? (please see details below)?
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);


No comments:

Post a Comment