Pixel-Composer/scripts/string_decimal/string_decimal.gml

57 lines
1.3 KiB
Text
Raw Normal View History

2023-07-12 16:28:32 +02:00
function string_variable_valid(str) {
static valid_char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
for( var i = 1; i <= string_length(str); i++ ) {
var cch = string_char_at(str, i);
if(string_pos(cch, valid_char) == 0) return false;
}
return true;
}
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);
2023-07-11 20:36:44 +02:00
if(dec == 0) return (neg? "-" : "") + string_digits(str);
2023-01-17 08:11:55 +01:00
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-07-11 20:36:44 +02:00
return (neg? "-" : "") + string_digits(pre) + "." + string_digits(pos);
2022-01-13 05:24:03 +01:00
}
function toNumberFast(str) {
2023-11-08 08:38:04 +01:00
INLINE
var r = real(str);
if(is_real(r)) return r;
return 0;
}
2022-01-13 05:24:03 +01:00
function toNumber(str) {
2023-11-08 08:38:04 +01:00
INLINE
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;
2023-07-11 20:36:44 +02:00
str = string_trim(str);
2023-04-15 14:48:29 +02:00
return str == string_decimal(str);
2022-01-13 05:24:03 +01:00
}