void setup() {
Serial.begin(9600);
}
void loop() {
long dividend = 12345;
long divisor = 123456;
int decimal_places = 1000;
char result[decimal_places+2]; // +2 for decimal point and null terminator
memset(result, 0, sizeof(result)); // initialize result to all zeroes
// Handle negative values
bool negative = false;
if ((dividend 0) || (dividend > 0 && divisor < 0)) {
negative = true;
}
dividend = abs(dividend);
divisor = abs(divisor);
// Calculate integer part of quotient
long quotient = dividend / divisor;
ltoa(quotient, result, 10);
// Add decimal point
strcat(result, ".");
// Calculate decimal part of quotient
long remainder = dividend % divisor;
for (int i = 0; i < decimal_places; i++) {
remainder *= 10;
quotient = remainder / divisor;
remainder = remainder % divisor;
result[i+2] = quotient + '0';
}
// Print result
if (negative) {
Serial.print("-");
}
Serial.println(result);
while (1) {} // stop program from continuing indefinitely
}
Like this:
Like Loading...
Related