Pixel-Composer/scripts/string_decimal/string_decimal.gml

34 lines
926 B
Plaintext
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-04-16 16:24:17 +02:00
gml_pragma("forceinline");
2023-05-03 21:42:17 +02:00
if(is_real(str)) return str;
if(!isNumber(str)) return 0;
2023-01-17 08:11:55 +01:00
2023-02-19 02:13:19 +01:00
var expo = 0;
if(string_pos("e", str)) {
var pos = string_pos("e", str);
2023-03-25 12:27:04 +01:00
expo = real(string_copy(str, pos + 1, string_length(str) - pos));
2023-02-19 02:13:19 +01:00
}
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);
2023-04-15 14:48:29 +02:00
}
function isNumber(str) {
if(is_real(str)) return true;
return str == string_decimal(str);
2022-01-13 05:24:03 +01:00
}