Search found 2 matches

by taurous
Tue Oct 23, 2012 7:28 pm
Forum: Programming Discussion
Topic: String to Float
Replies: 3
Views: 2016

Re: String to Float

Thanks so much for the feedback! I'll take a crack at seeing how I can simplify it more. EDIT: I worked on simplifying and optimizing it and I came up with this: float stringToFloat(std::string str, bool ignoreLetters) { float number = 0; int sign = 1; int decLength = 0; for (unsigned short i = 0; i...
by taurous
Tue Oct 23, 2012 7:28 am
Forum: Programming Discussion
Topic: String to Float
Replies: 3
Views: 2016

String to Float

So I came up with a method to convert a float to a string: float stringToFloat(std::string str, float defaultVal = 0) { float decimal = 0; int integer = 0; int sign = 1; for (int i = str.size()-1; i >= 0; i--) { int digit = (int)str[i] - 48; if (digit >= 0 && digit < 10) { decimal+=digit; if...