Giter Site home page Giter Site logo

Comments (2)

jeaiii avatar jeaiii commented on May 26, 2024

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.

jk-jeon avatar jk-jeon commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.