Skip to content

Public API for counting digits? #112950

Answered by stephentoub
thfsilvab asked this question in Q&A
Discussion options

You must be logged in to vote

Using the default format, formatting a uint will never exceed ten digits, so you can just format into temporary stack space, e.g.

Span<char> formatted = stackalloc char[10];
theUInt32Value.TryFormat(formatted, out int numDigits);

then, bonus, you already have the digits, which you can just copy from formatted.Slice(0, numDigits) into your destination buffer.

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@thfsilvab
Comment options

@tannergooding
Comment options

@neon-sunset
Comment options

Answer selected by thfsilvab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants