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
  • My problem is:
    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.
    I need this value.
    Idea: I convert these two values and add them with a comma to a string value. That works but if I want to convert this string back to a float value, the compiler converts the comma into an ASCII sign.

    I don't know what a MDU is. Does it return int's or strings? If they're strings you should use Andy's approach, converting the numbers to strings, concatenate them with a comma/point in between and converting the result back to a float.

    If you get numbers from your "MDU", you might be able to do this?
    (float) (5 + (0.1 * 2))
    i.e.
    (float) (MDU.component1 + (0.1 * MDU.component2))

    As I see it, you want to convert the second MDU component to a fraction?

    Regards,
    Joost Leeuwesteijn

Reply
  • My problem is:
    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.
    I need this value.
    Idea: I convert these two values and add them with a comma to a string value. That works but if I want to convert this string back to a float value, the compiler converts the comma into an ASCII sign.

    I don't know what a MDU is. Does it return int's or strings? If they're strings you should use Andy's approach, converting the numbers to strings, concatenate them with a comma/point in between and converting the result back to a float.

    If you get numbers from your "MDU", you might be able to do this?
    (float) (5 + (0.1 * 2))
    i.e.
    (float) (MDU.component1 + (0.1 * MDU.component2))

    As I see it, you want to convert the second MDU component to a fraction?

    Regards,
    Joost Leeuwesteijn

Children
  • "If they're strings you should use Andy's approach, converting the numbers to strings,"

    No!

    He wants to do arithmetic. Doing arithmetic requires numerical values.

    Therefore, if this "MDU" thing returns strings, he needs to convert those strings to numerical values!

    Then he can do his arithmetic with those numerical values.

    He might then need to convert the resulting numerical value to a string for display, transmission, or whatever...

  • No!

    He wants to do arithmetic. Doing arithmetic requires numerical values.

    Therefore, if this "MDU" thing returns strings, he needs to convert those strings to numerical values!

    Then he can do his arithmetic with those numerical values.

    You're right. I mistyped two solutions into one.

    If he gets strings:
    - convert to numbers and do arithmetic
    - concatenate with point inbetween, then convert to float and do arithmetic

    If he gets numbers:
    - do arithemetic
    - or less efficient way: convert numbers to strings, concatenate with point inbetween, convert back to float; but this doesn't make much sense but I believe that's what he (OP) had in mind.

    --
    Joost Leeuwesteijn

  • Sounds more like a class assignment. Given two numbers, which happen to be represented as strings, add them together.

    It is possible to do this without converting the strings to integers and back. (Though, using existing libraries likely would be the easiest and fastest way!). You could write code to loop over the strings, from the end to the beginning (least significant characters first), adding each individual pair of characters, and keeping the result as a character. That is, the same algorithm you were probably taught to use when doing the arithmetic by hand.

    Skipping all sorts of error and bounds checking, this should do:
    sprintf (outStr, "%d", atoi(inStr1) + atoi(inStr2));