Public API for counting digits? #112950
-
I was looking for functions in the framework or runtime to count the digits of To give context, I need to allocate a buffer for a string that I don't want to trim at the end because of overallocation. I came across FormattingHelpers.CountDigits.cs but it's internal and used at Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
There is no such public API. Even if exposed, it's likely to get a different name. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
Using the default format, formatting a uint will never exceed ten digits, so you can just format into temporary stack space, e.g.
then, bonus, you already have the digits, which you can just copy from
formatted.Slice(0, numDigits)
into your destination buffer.