1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

convert £786 to string

Discussion in 'Programming' started by RizbIT, Feb 12, 2020.

  1. RizbIT

    RizbIT Member

    Joined:
    Jun 24, 2016
    Posts:
    509
    real("£786") crashes the program

    so whats the best way to handle this?

    some users may enter "$786" some may enter "77.77"

    i can use string_digits but that doesnt accomodate 77.77 that will give 7777
     
  2. O.Stogden

    O.Stogden Member

    Joined:
    Jul 25, 2018
    Posts:
    247
    string() converts things to a string.

    real() converts to a number.

    So string("£786") should return "£786".

    EDIT: Like Kupo15 said, it's best to just restrict the user to entering how you want them to enter.

    It's difficult to give advice on this without context, but most times when you're entering text in a game or app, the fields only accept certain input.
     
  3. kupo15

    kupo15 Member

    Joined:
    Jun 20, 2016
    Posts:
    925
    I'd be curious about the best solution to this as well, though my suggestion is a design one. Since you are expecting currency you can make rules about what gets inputted and how to interpret the strict real conversion without the dot. Is it necessary to have the player input a currency symbol and decimal point? Why not set up a system where the decimal point is fixed and the user just needs to enter numbers? 2 digits are only cents, 3 is ones dollar and 2 cents etc...

    Also a silly sounding question, but why do you need to convert to real? I'm making an app that is dealing with money entry and I thought I needed to convert to real but leaving everything as a string works perfectly fine and is better than converting for what I'm doing

    He's asking about real("$315.0") not string() which crashes because you can't convert $ to real.
     
  4. O.Stogden

    O.Stogden Member

    Joined:
    Jul 25, 2018
    Posts:
    247
    He says real() crashes the program, not string().

    I thought he was wanting the numbers converted to a string along with the symbol, my bad.
     
  5. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    1,106
    You could just remove the "£" from the start of the string:
    Code:
    if string_digits(string_char_at(str, 1)) = ""
    {str = string_delete(str, 1, 1)}
    val = real(str)
    Or read through the string and remove any characters that aren't a digit, dot, comma or minus sign:

    Code:
    var str = argument0, i = 0, c, new_str = "";
    repeat string_length(str)
    {
    c = string_char_at(str, ++i)
    if c = "."
    || c = "-"
    || c = ","
    || string_digits(c) != ""
    {new_str += c}
    }
    return new_str
    Or you could do:

    Code:
    var str = argument0, i = 0, c;
    repeat string_length(str)
    {
    c = string_char_at(str, ++i)
    if string_digits(c) = ""
    && c != "."
    && c != -
    && c != ,
    {str = string_delete(str, i--, 1)}
    }
    return str
     
    Last edited: Feb 12, 2020
    Amon likes this.
  6. kupo15

    kupo15 Member

    Joined:
    Jun 20, 2016
    Posts:
    925
    He wants to remove the currency symbol but retain the decimal point though

    The best solution for that would be to simply delete the first character of the string then convert to real. Then again, still waiting on the context of the string from there OP. If it's part of a larger string then it would be more involved
     
  7. RizbIT

    RizbIT Member

    Joined:
    Jun 24, 2016
    Posts:
    509
    real("$315.0") should just give 315 or 315.0
     
  8. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,951
    Rather than worrying about what to remove (which is an indefinite amount of characters), worry about what to keep. That's numbers, dots, commas, plus and minus signs... anything that can be parsed as a number. Filter out anything that isn't this.
     
    RizbIT and kupo15 like this.
  9. RizbIT

    RizbIT Member

    Joined:
    Jun 24, 2016
    Posts:
    509
    Could there not be a simple built in function get_number("£500.44") to give "500.44"
     
  10. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    1,106
    Kind of, string_replace_all("£500.44", "£", "")
     

Share This Page