Thread: How to do a particular conversion?

1. 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
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.  Reply With Quote

2. 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.  Reply With Quote

3. 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.  Reply With Quote

4. Originally Posted by laserlight 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.  Reply With Quote

5. Originally Posted by laserlight 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?  Reply With Quote

6. Read up on printf  Reply With Quote

7. 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.  Reply With Quote

8.  Reply With Quote

9. Originally Posted by Structure 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!  Reply With Quote

10. atof converts to floating point, not fixed point.  Reply With Quote

11. Originally Posted by laserlight 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.  Reply With Quote

12. 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!  Reply With Quote

13. [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.  Reply With Quote

14.   Reply With Quote

15. Originally Posted by flp1969 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.  Reply With Quote

Popular pages Recent additions Tags for this Thread

cntr, conversion, initiating, printing, result 