struct2x icon indicating copy to clipboard operation
struct2x copied to clipboard

GenericReader::convertDouble need consider '-'

Open datouxyz opened this issue 1 year ago • 0 comments

double GenericReader::convertDouble(const char* value, uint32_t length) { if (!length) return 0.0;

	bool isNegative = false; // Flag to indicate if the number is negative
	double result = 0.0;
	uint32_t startIdx = 0; // Start index for processing the number

	// Check for negative sign
	if (value[0] == '-') {
		isNegative = true;
		startIdx = 1; // Start processing from the next character
	}

	for (uint32_t idx = startIdx, bFlag = false, num = 0; idx < length; ++idx) {
		const char& c = value[idx];
		if (c == '.') {
			bFlag = true;
			continue;
		}
		uint8_t n = c - '0';
		if (!bFlag) {
			result *= 10;
			result += n;
		}
		else {
			++num;
			result += decimal(n, num);
		}
	}

	// Negate the result if the number is negative
	if (isNegative) {
		result = -result;
	}

	return result;
}

datouxyz avatar Jul 16 '24 14:07 datouxyz