This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Add two strings

Hallo,
Is it possible, that I can add the strings '1' '.' and '2'.
I want to add this 3 strings: 1.2 and convert it into a float value and than I want add a float value.

Parents
  • You said:
    "I get form the MDU 2 components. One component before and on after the comma. I must add them to a float value.
    Example: before: 5 after: 2
    The value of my division is 5.2."

    If read this as:
    "I get from the MDU this string "5,2" and I wish to convert them to a number."
    then I would say you
    float a;
    char MDU[4];
    strcpy(MDU,"5,2");
    MDU[1]='.';
    a = atof(MDU);
    a will equal 5.2
    In this case I replace the "," with a "." since that is what my atof expects. Maybe yours can just handle the "," as though it were the radix. Some places use "." for the radix and some use ",".

Reply
  • You said:
    "I get form the MDU 2 components. One component before and on after the comma. I must add them to a float value.
    Example: before: 5 after: 2
    The value of my division is 5.2."

    If read this as:
    "I get from the MDU this string "5,2" and I wish to convert them to a number."
    then I would say you
    float a;
    char MDU[4];
    strcpy(MDU,"5,2");
    MDU[1]='.';
    a = atof(MDU);
    a will equal 5.2
    In this case I replace the "," with a "." since that is what my atof expects. Maybe yours can just handle the "," as though it were the radix. Some places use "." for the radix and some use ",".

Children
  • If you read it as "I get from the MDU this string "5,2" and I wish to convert them to a number."

    Then surely the simplest approach is:

    float float_val;
    
    float_val =  MDU[0]-'0';      // Numerical value of the 1st character;
                                  // ie, the units digit
    
    float_val += (MDU[2]-'0')/10; // Numerical value of the 3rd character;
                                  // ie, the tenths digit
    

  • Then surely the simplest approach is:

    Simple it is. But also, unfortunately, wrong. One would have to divide by 10.0 instead of 10, or multiply by 0.1 to get the desired effect.

    And that's before considering that floating-point arithmetic on an embedded CPU like the 251 is generally a dubious choice --- putting it mildly. It's best to either avoid it completely, or delegate that part of the job to a tools suited for it: a DSP.

  • "Simple it is. But also, unfortunately, wrong. One would have to divide by 10.0 instead of 10"

    Oops - so one would!

    "floating-point arithmetic on an embedded CPU like the 251 is generally a dubious choice"

    That's my excuse - I never touch the stuff, so missed the obvious detail...
    ;-)

    Adds another side the "Sledgehammer to crack nut" title, though!

    One way to avoid floating-point is to choose one's units appropriately; eg, instead of using "5.2 whatsits", use "52 centi-whatsits" or 520 milli-whatsits" - and then use integer arithmetic.

    eg, in this case:

    int num_val;
    
    num_val =  (MDU[0]-'0')*10; // Numerical value of the 1st character;
                                // ie, the tens digit in centi-whatsits
    
    num_val += (MDU[2]-'0');  ; // Numerical value of the 3rd character;
                                // ie, the units digit in centi-whatsits
    

    or

    int num_val;
    
    
    num_val = (MDU[0]-'0')*100; // Numerical value of the 1st character; // ie, the hundreds digit in milli-whatsits
    num_val += (MDU[2]-'0')*10; // Numerical value of the 3rd character; // ie, the tens digit in milli-whatsits

  • Adds another side the "Sledgehammer to crack nut" title, though!

    One way to avoid floating-point is to choose one's units appropriately; eg, instead of using "5.2 whatsits", use "52 centi-whatsits" or 520 milli-whatsits" - and then use integer arithmetic.

    Erm, at the risk of being a pest, I'm afraid your blow missed the nut by an order of magnitude, which is bad even for a sledgehammer: 5.2 whatsits is 520 centi-whatsits.

    But I'm willing to forgive people educated on "imperial" units for mixing up their SI prefixes --- as long as they extend similar courtesy to people baffled by the relation between inches and pints. ;-)

  • What can I say? Guitly as charged, m'lud.

    "as long as they extend similar courtesy to people baffled by the relation between inches and pints"

    Imperial pints not to be confused with US pints, of course...

  • Of course, my milli-whatsits were out by the same order of magnitude.