Thread: conversion of char to decimal

  1. #1
    Registered User
    Join Date
    Nov 2008

    conversion of char to decimal


    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. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    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 %s for string and %lf for double

  3. #3
    Registered User
    Join Date
    Nov 2008
    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[35], 11); Where record[35] 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

    char amountx[12];

    char *amount;

    if (!(*cur))
    narrative = (*cur)->narrative;
    amount = (*cur)->amount;
    date = (*cur)->date;
    amountx[11] = amount[10];
    amountx[10] = amount[9];
    amountx[9] = '.';

    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


    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. #4
    Registered User
    Join Date
    Sep 2006
    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:

    i = 0;
    while(stringArray[i] == 0)  {
       for(j = 1; j < 12; j++)
          stringArray[j-1] = stringArray[j];
    Haven't run this, but it looks OK.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Sorry Adak,

    I had pretty much forgotten about this thread. Thanks for the advice.

    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 subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. invalid char to char conversion?
    By kryptkat in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2007, 05:16 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 09:51 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM