Pixel-Composer/scripts/string_decimal/string_decimal.gml

27 lines
775 B
Text
Raw Normal View History

2022-01-13 05:24:03 +01:00
function string_decimal(str) {
2023-01-17 08:11:55 +01:00
var neg = string_char_at(str, 1) == "-";
if(neg) str = string_copy(str, 2, string_length(str) - 1);
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
var dec = string_pos(".", str);
var pre = string_copy(str, 1, dec - 1);
var pos = string_copy(str, dec + 1, string_length(str) - dec);
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
return (neg? "-" : "") + (dec? string_digits(pre) + "." + string_digits(pos) : string_digits(str));
2022-01-13 05:24:03 +01:00
}
function toNumber(str) {
2023-01-17 08:11:55 +01:00
if(is_real(str)) return str;
2023-02-19 02:13:19 +01:00
var expo = 0;
if(string_pos("e", str)) {
var pos = string_pos("e", str);
expo = toNumber(string_copy(str, pos + 1, string_length(str) - pos));
}
str = string_replace_all(str, ",", ".");
2022-01-13 05:24:03 +01:00
str = string_decimal(str);
if(str == "") return 0;
if(str == ".") return 0;
2023-02-19 02:13:19 +01:00
if(str == "-") return 0;
return real(str) * power(10, expo);
2022-01-13 05:24:03 +01:00
}