且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用星号填充值

更新时间:2023-11-26 13:09:34

同样,我没有任何标准,只要我测试过它(假定固定宽度填充):

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]==',' )
        continue;
    if(chars[i] != '0')
        break;
    chars[i] = '*';
}
string paddedText = new string(chars);





虽然可以做一些重构 - 但是对于separtors来说有魔法字符串(来可以是从 cultureInfo.NumberFormat 获得,并且至少不处理负数。我还要抽象出扩展方法。



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.


虽然有一个 String.PadLeft [ ^ ]放入星星的方法。

也许,我会做什么使用PadLeft用星星填充未使用的空间,然后使用String.ToCharArray获取字符并手动填写千位分隔符。

然后只使用字符串构造函数将char数组作为将其转换回字符串的参数。
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.