diff --git a/src/raylib.h b/src/raylib.h index 108ab0245..7ebd4cdfe 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1416,7 +1416,8 @@ RLAPI int TextFindIndex(const char *text, const char *find); RLAPI const char *TextToUpper(const char *text); // Get upper case version of provided string RLAPI const char *TextToLower(const char *text); // Get lower case version of provided string RLAPI const char *TextToPascal(const char *text); // Get Pascal case notation version of provided string -RLAPI int TextToInteger(const char *text); // Get integer value from text (negative values not supported) +RLAPI int TextToInteger(const char *text); // Get integer value from text +RLAPI float TextToFloat(const char* text); // Get float value from text //------------------------------------------------------------------------------------ // Basic 3d Shapes Drawing Functions (Module: models) diff --git a/src/rtext.c b/src/rtext.c index a7d9903a8..0460d734b 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1389,6 +1389,40 @@ int TextToInteger(const char *text) return value*sign; } +// Get float value from text +float TextToFloat(const char* text) +{ + float value = 0.0f; + float floatingPoint = 0.0f; + int sign = 1; + + if ((text[0] == '+') || (text[0] == '-')) + { + if (text[0] == '-') sign = -1; + text++; + } + + for (int i = 0; (((text[i] >= '0') && (text[i] <= '9')) || text[i] == '.'); i++) + { + if (text[i] == '.') + { + if (floatingPoint > 0.0f) break; + + floatingPoint = 10.0f; + continue; + } + if (floatingPoint > 0.0f) + { + value += (float)(text[i] - '0') / floatingPoint; + floatingPoint *= 10.0f; + } + else + value = value * 10.0f + (float)(text[i] - '0'); + } + + return value * sign; +} + #if defined(SUPPORT_TEXT_MANIPULATION) // Copy one string to another, returns bytes copied int TextCopy(char *dst, const char *src)