Thread: How to do a particular conversion?

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    137

    How to do a particular conversion?

    Code:
    FILE *C;
        C = fopen("data2","rb");
        ??? result; // initiating
        int cntr = 0;
        for (cntr=1; cntr < last+1;cntr++)
        {
            fseek(C, sizeof(struct A) * (cntr-1), SEEK_SET); // mod 1
            fread(&B,sizeof(struct A),1,C);
            result = conversion of B.euros, read from the file, ex a structure, to two-decimal numbers
            printf("%????",result); // conversion
            getchar();//for trial purposes -- printing
        }
    I would please like to know how to do the initiating, conversion and printing, etc. The dimension of "result" need not be >= 100,000.00. Making available B.euros from the file is no problem. An example of a value read from the file is 15.75 as char and that format I wish to maintain; i.e., two decimal number so I can do further arithmetic with that number.
    Last edited by zach; 09-08-2019 at 02:44 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,065
    What is struct A?

    EDIT:
    What is B.euros?

    Frankly, it sounds like your issue is concerned with converting from some member of struct A to a fixed point arithmetic result. It has nothing to do with reading from file, so I will get rid of that. You can add it back in later for your real program, but for now it distracts from your problem.
    Last edited by laserlight; 09-08-2019 at 02:50 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,065
    Quote Originally Posted by zach
    An example of a value read from the file is 15.75 as char and that format I wish to maintain; i.e., two decimal number so I can do further arithmetic with that number.
    Two approaches:
    • Remove the '.' from the string then convert it to an integer. Now you deal with integers that are 100 times the actual value, so when you want to print, you just do n / 100 to get the part before the decimal point and n % 100 to get the part after the decimal point. Of course, you have to keep in mind the max value for the integer type.
    • Use a fixed point arithmetic library or write your own.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2019
    Posts
    137
    Quote Originally Posted by laserlight View Post
    Two approaches:
    • Remove the '.' from the string then convert it to an integer. Now you deal with integers that are 100 times the actual value, so when you want to print, you just do n / 100 to get the part before the decimal point and n % 100 to get the part after the decimal point. Of course, you have to keep in mind the max value for the integer type.
    • Use a fixed point arithmetic library or write your own.
    Yes I had considered your option one, but thought I should go for a type that has a broader value range. But OK, I will go for integers in the first place. I wouldn't know about a special library. Thank you for your response.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Posts
    137
    Quote Originally Posted by laserlight View Post
    Two approaches:
    • Remove the '.' from the string then convert it to an integer. Now you deal with integers that are 100 times the actual value, so when you want to print, you just do n / 100 to get the part before the decimal point and n % 100 to get the part after the decimal point. Of course, you have to keep in mind the max value for the integer type.
    • Use a fixed point arithmetic library or write your own.
    I decided to stick to integers all through and to convert outputs to strings, then flipper in periods / decimal points. Is there an easy way to right adjust a column in C?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,652
    Read up on printf
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Aug 2019
    Posts
    137
    I found an excellent article on the Net by Professor Don Colton (University of Hawaii) Secrets of "printf". Had no idea there was so much to this statement. I have a nice list now. Thank you for your economic suggestion.

  8. #8

  9. #9
    Registered User
    Join Date
    Aug 2019
    Posts
    137
    I decided to stick to integers all the way through, then at the end to convert to string and flipper in a decimal point with a simple routine I wrote. But I will certainly study the reference you gave me. Thank you!

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,065
    atof converts to floating point, not fixed point.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,405
    Quote Originally Posted by laserlight View Post
    Two approaches:
    • Remove the '.' from the string then convert it to an integer. Now you deal with integers that are 100 times the actual value, so when you want to print, you just do n / 100 to get the part before the decimal point and n % 100 to get the part after the decimal point. Of course, you have to keep in mind the max value for the integer type.
    • Use a fixed point arithmetic library or write your own.
    A potential third approach it to not even convert to integer (or float, or anything other than characters) ever... until and unless a calculation is needed. If a calculation is not needed then just output the characters.

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    496
    I have this policy to never use floating point if I can... Here's why... A simple approach to this "conversion" would be:
    Code:
    #define _GNU_SOURCE
    #include <stdint.h>
    #include <inttypes.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>   // for basename().
    #include <ctype.h>
    #include <errno.h>
    
    int main(int argc, char **argv)
    {
      char *err;
      double t;       /* double have 53 bits precision. 
                         Maximum value (with cents): +/-90071992547409.91 */
      int64_t i;
      int f;
    
      if ( ! *++argv )
      {
        fprintf( stderr, "Usage: %s \"<value>\"\n", basename( *(argv - 1) ) );
        return EXIT_FAILURE;
      }
    
      /* Example of conversion, testing for errors */
      errno = 0;
      t = strtod(*argv, &err);
      if ( ( *err && !isdigit(*err) ) || errno == ERANGE )
      {
        fputs( "ERROR: Cannot convert.\n", stderr );
        return EXIT_FAILURE;
      }
    
      i = t;                 // holds the integer part
      f = ( t - i ) * 100;   // holds the fractional part (cents).
    
      printf( "%" PRIi64 ".%d\n", i, f );
    
      return EXIT_SUCCESS;
    }
    If you run the program, as below:
    Code:
    $ ./test 3.14
    3.14
    $ ./test.9.44
    9.44
    $ ./test 9.ab
    ERROR: Cannot convert.
    But if you do:
    Code:
    $ ./test 9.3
    9.29
    Why? 0.3 isn't exact in binary floating point!
    Last edited by flp1969; 09-10-2019 at 07:57 AM.

  13. #13
    Registered User
    Join Date
    Aug 2019
    Posts
    137
    [QUOTE=flp1969;1289434]I have this policy to never use floating point if I can... Here's why... A simple approach to this "conversion" would be:
    etc.

    As I have responded elsewhere, (given that I was writing a financial program) I used integers, converting to string at the end, then inserting a decimal point. So no multiplying and dividing by 100 either.

  14. #14
    Registered User
    Join Date
    Feb 2019
    Posts
    496
    How to do a particular conversion?-show-me-what-you-got-smaller-jpg

  15. #15
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    653
    Quote Originally Posted by flp1969 View Post
    But if you do:
    Code:
    $ ./test 9.3
    9.29
    Why? 0.3 isn't exact in binary floating point!
    That's because you didn't properly round t when computing i and f. What you ended up with is essentially floor(t * 100.0) / 100.0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bcc conversion
    By sjhawar in forum C Programming
    Replies: 1
    Last Post: 06-12-2011, 01:50 PM
  2. C# to C++ conversion?
    By hammari in forum C# Programming
    Replies: 8
    Last Post: 03-18-2011, 12:51 AM
  3. C to C++ conversion
    By racerday182 in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2008, 06:20 PM
  4. Conversion
    By justgotthis in forum C Programming
    Replies: 4
    Last Post: 10-05-2005, 06:37 AM
  5. Conversion from C++ to C
    By MasterGBC in forum C Programming
    Replies: 4
    Last Post: 04-04-2003, 01:19 AM

Tags for this Thread