Pixel-Composer/scripts/string_decimal/string_decimal.gml

56 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 toNumber(str) {
2024-04-20 05:00:28 +02:00
if(is_numeric(str)) return str;
2024-04-01 11:10:01 +02:00
try { return real(str); }
catch(e) {}
2023-01-17 08:11:55 +01:00
2024-04-01 11:10:01 +02:00
return 0;
2023-04-15 14:48:29 +02:00
}
2024-10-06 11:23:58 +02:00
function toNumberFull(str) {
if(is_real(str)) return str;
var expo = 0;
if(string_pos("e", str)) {
var pos = string_pos("e", str);
expo = real(string_copy(str, pos + 1, string_length(str) - pos));
str = string_copy(str, 1, pos - 1);
}
str = string_decimal(str);
if(str == "") return 0;
if(str == ".") return 0;
if(str == "-") return 0;
return real(str) * power(10, expo);
}
2024-04-01 11:10:01 +02:00
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
}