Click here to Skip to main content
15,072,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anybody have a code snippet where I can easily display a numeric value with leading asterisks?

I am using:
IFormatProvider myFormatProvider = new System.Globalization.CultureInfo("nl-NL").NumberFormat;

to give me the CultureInfo I want to use, and I can get a value to display a numeric value in the correct format, for example:
theNumberToConvert.ToString("N4", myFormatProvider);

which will give me, for example:


...but how would I get this value to pad out this value to something like:

**.***.**10.000.000,0000 ?

Again, I've nothing standard, this works as far as I've tested (assumes fixed width padding):
var cultureInfo = new System.Globalization.CultureInfo("nl-NL");
var numberFormatProvider = cultureInfo.NumberFormat;
var chars = theumber.ToString("0000,0000,000,000.#####",  numberFormatProvider).ToCharArray();
for (int i=0; i< chars.Length; i++)
    if(chars[i]=='.'  || chars[i]==',' )
    if(chars[i] != '0')
    chars[i] = '*';
string paddedText = new string(chars);

Could do with some refactoring though - has magic strings for the separtors (come can be got from cultureInfo.NumberFormat) and doesn't handle negatives at the very least. I'd also abstract out to en extension method.
penguin5000 7-Aug-14 8:35am
Thanks. I had pretty much resigned myself to having to do something like that, and was just hoping that there was something a bit more concise that I wasn't aware of.
There is nothing standard I know of to do that, although there is a String.PadLeft[^] method which would put in the stars.
Probably, what I would do is use PadLeft to fill the unused space with stars, then use String.ToCharArray to get the characters and fill in the thousands separator manually throughout.
Then just use the string constructor that takes a char array as a parameter to convert it back to a string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900