diff --git a/cocos/base/ccUtils.cpp b/cocos/base/ccUtils.cpp index 5a025b25ec..36cafb1c20 100644 --- a/cocos/base/ccUtils.cpp +++ b/cocos/base/ccUtils.cpp @@ -24,6 +24,9 @@ THE SOFTWARE. ****************************************************************************/ #include "base/ccUtils.h" + +#include + #include "base/CCDirector.h" #include "renderer/CCCustomCommand.h" #include "renderer/CCRenderer.h" @@ -160,6 +163,27 @@ std::vector findChildren(const Node &node, const std::string &name) return vec; } + +#define MAX_ITOA_BUFFER_SIZE 256 +double atof(const char* str) +{ + if (str == nullptr) + { + return 0.0; + } + + char buf[MAX_ITOA_BUFFER_SIZE]; + strncpy(buf, str, MAX_ITOA_BUFFER_SIZE); + + // strip string, only remain 7 numbers after '.' + char* dot = strchr(buf, '.'); + if (dot != nullptr && dot - buf + 8 < MAX_ITOA_BUFFER_SIZE) + { + dot[8] = '\0'; + } + + return ::atof(buf); +} } diff --git a/cocos/base/ccUtils.h b/cocos/base/ccUtils.h index 939d71b854..1fba5b1b40 100644 --- a/cocos/base/ccUtils.h +++ b/cocos/base/ccUtils.h @@ -73,6 +73,11 @@ namespace utils * @since v3.2 */ std::vector findChildren(const Node &node, const std::string &name); + + /** Same to ::atof, but strip the string, remain 7 numbers after '.' before call atof。 + * Why we need this? Because in android c++_static, atof ( and std::atof ) is unsupported for numbers have long decimal part and contain several numbers can approximate to 1 ( like 90.099998474121094 ), it will return inf. this function is used to fix this bug. + */ + double atof(const char* str); } NS_CC_END