Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    19
    Rep Power
    0

    Question currency problem!!!


    I can't instructions on how to actually create a variable in c. I want to create a real currency variable or at least use one that already exists and limit the decimal places to two. I am using glade and need to make it work in a GUI. monetary.h is not anywhere in my library and I want it for C, not c++. I just can't seem to find it anywhere. Please Help!

    Chips
  2. #2
  3. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    never heard of monetary.h, so I doubt you will find it for very many compilers.

    you can't set the number of decimal places in float or double variables, but you can print them with as many decimal places as desired.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    19
    Rep Power
    0
    Theres gotta be something I can do to adjust the variable before putting back in the text box. Do you know of any way to change the format of a varible to be a currency or how to create a new variable that would automatically be limited to only to decimal points. The same thing that setprecision(2). Would do. The $ symbol is not important. I have spent the last two days trying to find a way to accomplish this. It is for a project for school that does financial stuff and I want to do it right, not with a float that uses four or more decimals. I need only two (to the hundreths). It can be a char as well. monetary.h is supposed to be part of the CVS repository. And I mean data type (creating a new data type, uh). If not than how would you suggest? PLEASE HELP
  6. #4
  7. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    what text box do you mean? are you formatting the text yourself
    Code:
    float money = 12.50;  // $12.50US
    char text[20];
    // format text for 2 decimal places
    sprintf(text,"%.2f", money);
    // now you can do whatever you want with text
    cout << text << endl;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    19
    Rep Power
    0
    Not a command line program!! This is a GUI!! 'cout' ain't gonna cut it with this one. I am using Glade and the entry boxes. Everything was great until I realized that it was going over 2 decimal places!!
    As for the question. I need to format it without using cout or printf or any of that. I have to take the value from one entry box change the precision of the decimal and then put it back into the entry box. All changes to the value have to be done with a function after it is taken out and before it put back in. No displaying of the value in between or during the changes! I'm also trying to keep it all C so that it will be efficient as all get out I just have this one problem.
  10. #6
  11. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    I only used cout for example. do whatever you want with the formatted text. You wanted to know how to format a double/float, well in C that's the way to do it. Now I assume you can take that variable text and send it to your Glade program to be displayed on the GUI screen.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    19
    Rep Power
    0
    Windows freaks out and asks me to send an error report! Any other suggestions. I would be willing to make a whole new variable type if I could just find the destructions how. It couldn't take more than 2 days and I have already spent 2 days searching for a solution to my problem and I think creating something new might be the only option!
  14. #8
  15. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    post some code so we can see what you did.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    19
    Rep Power
    0
    Here it is: (seperated from all other code for testing) Now the darn message thing is giving me problems. It won't let me submit a reply

    Code:
    //NWC calculate button clicked
    void
    on_BT_NWCcalc_clicked                  (GtkButton       *button,
                                            gpointer         user_data)
    { 
      const float *test;
      char test2[];
      GtkWidget *testing1, *testing2;
      
      testing2 = lookup_widget (NetWorthWindow, "NWCliability1");
      test = gtk_entry_get_text (GTK_ENTRY (testing2));
      sprintf(test2,"%.2f", test);
      testing1 = lookup_widget (NetWorthWindow, "entNWCtotassets");
      gtk_entry_set_text (GTK_ENTRY(testing1), test);
    }
    actually its now just changing the whole thing to nothing but 0's, but it is printing 0.00 instead of just 0. Thank you for you help so far. If you know how to fix the rest cool, otherwise I'll keep looking, but please help.
  18. #10
  19. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    test2 is an unallocated pointer that points to never-never land. change it like this and it should work. test is another char pointer, which is not either a float or double. If it contains the money value as a text string, then you need to convert it to float, do whatever math you want on it, then convert back to text.
    Code:
    float money;
    char test2[20];
    //convert text string to float
    money = atof(text);
    money += .5; // add 50 cents (or do something else)
    // convert money back to text  string
    sprintf(test2,"%.2f", money);
    testing1 = lookup_widget (NetWorthWindow, "entNWCtotassets");
    gtk_entry_set_text (GTK_ENTRY(testing1), test2);
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    19
    Rep Power
    0
    Thats fantastic, but now a new one. Its popping out thing long number that has nothing to do with the number I originally entered! The other option is that it equals 0. How do I get it to put out the same thing only formatted. Heres the code:
    Code:
    //NWC calculate button clicked
    void
    on_BT_NWCcalc_clicked                  (GtkButton       *button,
                                            gpointer         user_data)
    { 
      float money;
      float *test;
      char test2[20];
    
      GtkWidget *testing1, *testing2;
      
      testing2 = lookup_widget (NetWorthWindow, "NWCliability1");
      test = gtk_entry_get_text (GTK_ENTRY (testing2));
      money = atof(test);
      money += .5;
      money -= .5;
      sprintf(test2,"%.2f", money);
      testing1 = lookup_widget (NetWorthWindow, "entNWCtotassets");
      gtk_entry_set_text (GTK_ENTRY(testing1), test2);
    }
    Please help me with this last part even though I'm pretty sure it has to do with the % tag that is being used.
  22. #12
  23. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    I don't understand the question. But you can delete these two lines
    Code:
      money += .5;
      money -= .5;
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    19
    Rep Power
    0
    OKay they're deleted and it did add and subtract the 50 cents, but it displays this long number that has nothing to do with the number that I put in. I'll put '122.456' and get back '222345.00'. How do I fix that.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    104
    Rep Power
    11
    I'm really no expert, just taking a shot-in-the-dark, but you declared test as "float *test" , a pointer to a float, then later on you use it in "atof(test)", which converts a string to a float. Maybe you meant to declare test as "char *test" ?
  28. #15
  29. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    Originally Posted by BenZarboni
    I'm really no expert, just taking a shot-in-the-dark, but you declared test as "float *test" , a pointer to a float, then later on you use it in "atof(test)", which converts a string to a float. Maybe you meant to declare test as "char *test" ?
    Oops! you're right.

    I don't know what gtk_entry_get_text() returns. In his original post, it looked like it returned char*. Now he has it returning float*. Which is the right return value???? If it returns float* then you don't need the atof() line.
    Code:
    sprintf(test2,"%.2f",*test);
    Last edited by Ancient Dragon; October 27th, 2004 at 04:41 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo