Comments (2)
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;
}
from itoa.
I wanted to see how hard it might be to extend this to get fixed length zero-padded responses.
@spannella It's actually explained at the end of the article linked in the README.
TL;DR
You can do this.
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.
from itoa.
Related Issues (12)
- How hard would it be to provide a C version of jeaiii_to_text.h? HOT 2
- warnings emitted by msvc HOT 4
- Issue in integer to string conversion HOT 3
- Versions for uint8_t, int8_t, uint16_t and int16_t HOT 1
- C conversion/adaptation of jeaiii_to_text.h compared to C conversion/adaptation of itoa_jeaiii.cpp not as much faster as hoped HOT 1
- Clarify roles of the two implementations HOT 2
- Clang missing-braces
- FYI, we're using this in MoarVM HOT 1
- GCC warning due to ASCII art HOT 7
- I don't think `n` can ever be < 10 here
- remove usage of template variable `mask` to support c++11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from itoa.