# Thread: conversion of char to decimal

1. ## conversion of char to decimal

All,

I have a problem and am struggling to be honest. I have a program that has a series of char values that are read from a file and stored in a database table. The program is using embedded sql.

I have an amount that is read from the file into char as 000000300000

This value represents 3000.00 in decimal or double or whatever value is best to use.

I want to use the value 3000.00. Is there a function that simply takes the char and moves it to the required format.

Can I simply use sprintf with a specific flag ? 2. 000000300000, meaning what? A char is a 1byte variable. It has a maximum value of 256 (-128/128 for unsigned).
If the above is a text file then you mean a sequence of characters, usually referred as sting. In that case you can use strtod() (google it) to convert a string to double.

Now, there is no way to understand that the last two digits need a period to have actually 0000003000.00. But that is no problem. Just divide by 100.

Give some code if you want or verify that you are actually doing what I suspect

edit: You can also use sscnaf() or scanf() to convert data to appropriate format. It is &#37;s for string and %lf for double 3. Well I suppose I could be a bit more specific :- Here goes ::

I have a question for you that you might be able to answer.

I have a string value amount that is read from a file as "00000300000" which actually means £3000 or the euro equivalent.

It is read from the datafile like so strncpy(amount, &record, 11); Where record is the position in the datafile.

It is manipulated later on in the program like so to produce the field with the decimal place.

Inside a function extract

declare
char amountx;

char *amount;

for(; {
if (!(*cur))
break;
narrative = (*cur)->narrative;
amount = (*cur)->amount;
date = (*cur)->date;
sprintf(amountx,"%s\n",amount);
amountx = amount;
amountx = amount;
amountx = '.';

which produces 000003000.00

What I want to do is now remove the leading zeros from this string and maybe even do some multiplication with it.

The result I want to achieve is to use the variable in an sql statement to compare it to a value stored as a decimal in the format

3000.00

for e.g where paymentamount = :amountx

Hopefully you may have a function or a command I can use that will strip the leading zeros or alternatively allow me to pad the paymentamount field to have zeros.

Hopefully not too confused. 4. So you don't want to use the string to double abilities that C has? Odd, but you can just put your string into a while loop to knock off the leading zero's:

Code:
```i = 0;
while(stringArray[i] == 0)  {
for(j = 1; j < 12; j++)
stringArray[j-1] = stringArray[j];
i++;
}```
Haven't run this, but it looks OK. I used atof to do it in the end. The where clause in the sql doesn't need to be explicit and as long as the variable becomes a double it works nicely.

double new_amount=0;
printf("A new_amount \t%g\n",new_amount += atof(amount)/10);

To check the value of new amount I can do a printf as follows :-

printf("B new_amount%8.2f\n",new_amount);

Speak soon.

;-) Popular pages Recent additions 