• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

convert £786 to string

RizbIT

Member
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
 

O.Stogden

Member
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.
 

kupo15

Member
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

So string("£786") should return "£786".
He's asking about real("$315.0") not string() which crashes because you can't convert $ to real.
 

O.Stogden

Member
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.
 

Joe Ellis

Member
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:

kupo15

Member
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
 

TsukaYuriko

☄️
Forum Staff
Moderator
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.
 
Top