You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to see how hard it might be to extend this to get fixed length zero-padded responses.
The other use-case is slightly less common but it can be often helpful to represent a fixed-point decimal number with a standard integer type like int32_t or int64_t where the number is stored as N * 10^D where D is the number of decimal digits supported. I was trying to think of how one could utilize your same approach but with the added issue of inserting the decimal place and potentially having leading zeros after the decimal but before the first digit.
Finally there is the fixed length, fixed-point decimal number, where you want to render the same number fixed-point decimal number but with a fixed number of decimal places and zero padding in the front for the rest
Mostly just thoughts on how this could be extended, I could provide naïve solutions to each of these if its helpful
The text was updated successfully, but these errors were encountered:
if you want to zero pad on the left, it may be better just to convert the number backwards, since you know the length ahead of time. Also, since you are writing out more digits than needed the speed advantage may be lost so I would tend to go with simple code.
One trick would be to add a large power of 10 like 1000000000 and print the number then zero out the leading 1, if you know all your numbers are less than 1e9. you could do the same trick to print base10 fixed point, something like the following for dollars.
char *to_text_as_dollars(char text[], unsigned int n)
{
auto dollars = n / 100;
auto cents = n % 100;
text[0] = '$';
auto tail = to_text_from_integer(to_text_from_integer(text + 1, dollars), cents + 100);
tail[-3] = '.'
return tail;
}
I think it will perform better than the naive way of producing the digits in backward. The naive way should compute both the quotient and the remainder (thus two multiplications) for a digit pair, but the approach of this repo only needs one multiplication per a digit pair.
I wanted to see how hard it might be to extend this to get fixed length zero-padded responses.
The other use-case is slightly less common but it can be often helpful to represent a fixed-point decimal number with a standard integer type like int32_t or int64_t where the number is stored as N * 10^D where D is the number of decimal digits supported. I was trying to think of how one could utilize your same approach but with the added issue of inserting the decimal place and potentially having leading zeros after the decimal but before the first digit.
Finally there is the fixed length, fixed-point decimal number, where you want to render the same number fixed-point decimal number but with a fixed number of decimal places and zero padding in the front for the rest
Mostly just thoughts on how this could be extended, I could provide naïve solutions to each of these if its helpful
The text was updated successfully, but these errors were encountered: