Fixed bug in asDouble but changed its operation. Decimal only.
This commit is contained in:
parent
75df8d0282
commit
974a509767
52
ZString.cpp
52
ZString.cpp
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
#include "ZString.h"
|
#include "ZString.h"
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
#include "Exception.h"
|
#include "Exception.h"
|
||||||
@ -125,7 +124,56 @@ namespace coreutils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
double ZString::asDouble() {
|
double ZString::asDouble() {
|
||||||
return strtod(cursor, &cursor);
|
double value = 0;
|
||||||
|
bool negative = false;
|
||||||
|
char *end = data + length;
|
||||||
|
if(*cursor == '-') {
|
||||||
|
negative = true;
|
||||||
|
++cursor;
|
||||||
|
} else if(*cursor == '+')
|
||||||
|
++cursor;
|
||||||
|
if(cursor == end)
|
||||||
|
return 0;
|
||||||
|
while((*cursor >= '0') && (*cursor <= '9')) {
|
||||||
|
value *= 10;
|
||||||
|
value += *cursor - 48;
|
||||||
|
++cursor;
|
||||||
|
if(cursor == end) {
|
||||||
|
if(negative)
|
||||||
|
return -value;
|
||||||
|
else
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(*cursor != '.')
|
||||||
|
if(cursor == end) {
|
||||||
|
if(negative)
|
||||||
|
return -value;
|
||||||
|
else
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
++cursor;
|
||||||
|
if(cursor == end) {
|
||||||
|
if(negative)
|
||||||
|
return -value;
|
||||||
|
else
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
double div = 10;
|
||||||
|
while((*cursor >= '0') && (*cursor <= '9')) {
|
||||||
|
value += (*cursor - 48) / div;
|
||||||
|
div *= 10;
|
||||||
|
++cursor;
|
||||||
|
if(cursor == end) {
|
||||||
|
if(negative)
|
||||||
|
return -value;
|
||||||
|
else
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(negative)
|
||||||
|
return -value;
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ZString::incrementCursor(int length) {
|
int ZString::incrementCursor(int length) {
|
||||||
|
@ -99,5 +99,8 @@ int main(int argc, char **argv) {
|
|||||||
std::cout << "[" << test18.find(".") << "] " << test18.parsed() << ":" << test18.unparsed() << std::endl;
|
std::cout << "[" << test18.find(".") << "] " << test18.parsed() << ":" << test18.unparsed() << std::endl;
|
||||||
test18.reset();
|
test18.reset();
|
||||||
std::cout << "[" << test18.find("x") << "] " << test18.parsed() << ":" << test18.unparsed() << std::endl;
|
std::cout << "[" << test18.find("x") << "] " << test18.parsed() << ":" << test18.unparsed() << std::endl;
|
||||||
|
|
||||||
|
coreutils::ZString test19("-16473.65476Z");
|
||||||
|
std::cout << test19.asDouble() << ":" << test19.unparsed() << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user