473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting currency to words just like for checks

how would I go about separating the decimal amount from that of the dollar.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include<stdlib.h>
  4.  
  5.  
  6.  
  7. int main(int argc, char *argv[]){
  8.  
  9.     char *one[] = {" zero "," one"," two"," three"," four"," five"," six"," seven","eight"," nine"," ten"," eleven"," twelve"," thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
  10.     char *tens[] = {"",""," twenty", " thirty"," forty", " fifty", " sixty", " seventy", " eighty", " ninety", " hundred"};
  11.  
  12.     char input[10];
  13.     //argument
  14.     strcpy(input, argv[1]);
  15.     int dollar;
  16.     int temp;
  17.     float decimal ;
  18.  
  19.  
  20.  
  21.     dollar = atoi(input);
  22.     printf("The dollar value is %i\n", dollar);
  23.  
  24.     decimal = atoi(input) - dollar;
  25.     printf("The decimal value is %3.2f\n", decimal);
  26.  
  27.     decimal = (double) decimal - dollar;
  28.  
  29.     if(dollar == 0){
  30.         printf("%s", one[0]);
  31.     }else{
  32.         if(dollar >= 100){
  33.                 temp = dollar / 100;
  34.                 dollar = dollar % 100;
  35.                 printf("The dollar value is %i \n", dollar);
  36.                 printf(" %s%s", one[temp], tens[10]);
  37.             }
  38.  
  39.             if (dollar >= 20){
  40.                 temp = dollar /10;
  41.                 dollar = dollar%10;
  42.  
  43.                 printf("%s ", tens[temp]);
  44.  
  45.             }if(dollar >= 0){
  46.                 temp = dollar;
  47.                 printf("%s ", one[temp]);
  48.  
  49.  
  50.  
  51.             }
  52.  
  53.  
  54.     }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. }
  63.  
Feb 20 '13 #1
3 1989
weaknessforcats
9,208 Expert Mod 8TB
If you have an amount, like 251, you get the dollars by 251/100, which is 2.

Next, to get the pennies, reduce the amount by the whole dollars from above. 251 - 2 * 100. which is 51.

Then display a dollar sign + dollar variable + decimal point + pennies:

$2.51

Only you know the decimal point was only displayed and really not part of the vslue anywhere.

Try to never use floating point for money problems. Floating point variables do rounding which can totally screw up a money problem.

Floating point is for scientific stuff where accuracy eed only be approximate.
Feb 20 '13 #2
Banfa
9,065 Expert Mod 8TB
In fairness in this situation the user is likely to enter the amount of money as floating point value e.g. "4.35" FOR $4.35 because that is how users think.

However weaknessforcats point about not using floating types holds true so how do you handle this string?

The answer is fairly simple treat it as 2 integers separated by a '.'.

The pseudo code looks something like

Expand|Select|Wrap|Line Numbers
  1. input = <valueFromUser>
  2. dollars = atoi(input)
  3. dotindex = findIndexOfDot(input)
  4. centsindex = dotindex + 1
  5. cents = atoi(&input[centsindex])
  6.  
  7. cents += dollars * 100 // optional if you want the full sum cents
  8.  
an alternative that uses a little string processing

Expand|Select|Wrap|Line Numbers
  1. input = <valueFromUser>
  2. digitsonly = input with '.' removed
  3. cents = atoi(digitsonly)
  4.  
or finally it wouldn't be too hard to write your own version of atoi that just ignores the '.'

In all cases you need to make sure the user has only entered 2 digits worth of cents.
Feb 20 '13 #3
This is what I ended up doing. Thanks for the help tho.


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include<stdlib.h>
  4.  
  5.  
  6.  
  7. int main(int argc, char *argv[]){
  8.  
  9.     char *one[] = {" zero "," one"," two"," three"," four"," five"," six"," seven","eight"," nine"," ten"," eleven"," twelve"," thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
  10.     char *tens[] = {"",""," twenty", " thirty"," forty", " fifty", " sixty", " seventy", " eighty", " ninety", " hundred"};
  11.  
  12.     char input[10];
  13.     //argument
  14.     strcpy(input, argv[1]);
  15.     float num = atof(input);
  16.     int dollar ;
  17.     int temp;
  18.     double decimal;
  19.  
  20.     //Currency Ammount
  21.     printf("Money: %.2f\n", num);
  22.  
  23.  
  24.  
  25.  
  26.  
  27.     //Separates whole number from decimal
  28.     dollar = (int)num;
  29.     printf("The dollar value is %d\n", dollar);
  30.  
  31.     //Separates decimal from whole number and prints as float
  32.     decimal = num -dollar;
  33.     printf("The decimal value is %.2f\n", decimal);
  34.  
  35.  
  36.  
  37.  
  38.     //prints out zero if amount is 0
  39.     if(dollar == 0){
  40.         printf("%s", one[0]);
  41.     }else{
  42.         //prints out hundred amount
  43.         if(dollar >= 100){
  44.                 temp = dollar / 100;
  45.                 dollar = dollar % 100;
  46.                 printf("The dollar value is %i \n", dollar);
  47.                 printf(" %s%s", one[temp], tens[10]);
  48.             }
  49.              //prints out tens place amount
  50.             if (dollar >= 20){
  51.                 temp = dollar /10;
  52.                 dollar = dollar%10;
  53.  
  54.                 printf("%s ", tens[temp]);
  55.              //prints out one place amount
  56.             }if(dollar >= 0){
  57.                 temp = dollar;
  58.                 printf("%s ", one[temp]);
  59.             //converst decimal to whole number and prints out as cent fraction
  60.             }if(decimal >0){
  61.                 decimal = (double)decimal *100;
  62.  
  63.                 printf("and %.0f \\100 \n", decimal);
  64.             }
  65.  
  66.  
  67.     }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. }
  76.  
Feb 20 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: alien2_51 | last post by:
At some point after running this report the "Wholesale Price" column data type in the excel spread sheet changes from Currency to General... It seems to ba around the 300th or so row but varies......
0
by: Marcelo López | last post by:
Hello everybody. I'm working with an office pivot table and i have to convert from its currency type to a decimal (in C#) , operate with the result and then re-convert it to the currency type to...
2
by: Dalan | last post by:
This should not be an issue, but it is. I'm sure that someone knows what little piece of code is needed too persuade Access 97 to include a currency format for labels (Avery, mailing type). Have...
1
by: Jon | last post by:
I have a field called "Amount". In it, there may be values populated that are either in £ or $. I want another field, called "ConvertedAmount" that will store the value in dollars. If Amount...
0
by: PLENI SELENE | last post by:
HOW TECHNOLOGY MOVES FROM TALK TO ELECTRONIC DEVICES TO SPEAK GOOD SPANISH! Would you Like To Know How The Electronic Devices Understand When You Talk to Them, And How It Works To...
4
by: sumanix | last post by:
Hi Guys and Girls!!!! I am very new to this game of programming and i would like some wise information on how to convert currencies in visual basic. It is a college project and i'm not getting...
3
by: Grzegorz =?iso-8859-2?Q?=A6lusarek?= | last post by:
Hi all. I have situation that I have value that holds price and I must show this price using national specification(e.g. thousands_sep). Any idea how this can be done under python 2.4.4? I saw that...
3
by: humayou | last post by:
I want to make a cheque print so i have the amount and i want to convert the amount in Currency & also in wording.. Kindly help me,
0
by: santoshsri | last post by:
Hello, I have written a Console application in C#, it basically imports data from tab delimited text files and save in SQl Server database. I am converting currency format of Mortgage value into...
1
by: sijugeo | last post by:
Hi all, Myself having a textbox which is indented to enter the Currency of a particular good, and its working fine. Now the problem is , I want to format the way...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.