我正在寻找一种客户十进制格式。如果我执行“(3003.3003d).ToString(#,###.00##”),我现在得到的是“3,003.3003”。我想要获得的格式是“3'003.300,3”。
我可以使用客户方法 math.floor 并自己将其分解,但我很好奇是否有一种方法可以使用文化信息来做我正在寻找的事情。有人知道这样做的方法吗?
用自定义 IFormatprovider 写了这个人作为答案,因为教程非常好,但我不相信当前的文化存在我需要的东西。这是我的最终答案:
private string ToCustomDecimal(decimal Input, int MaxLenPostDec, int MinLenPostDec)
{
string CustD;
string[] Split = Input.ToString().Split('.');
string BigNum = Split[0];
string SmallNum = "0";
if (Split.Length > 1)
SmallNum = Split[1];
int Spot = 0;
bool FirstD = false;
string OutBigNum = "";
for (int Locc = BigNum.Length - 1; Locc >= 0; Locc--)
{
Spot++;
if (FirstD == false)
{
OutBigNum = BigNum.Substring(Locc, 1) + OutBigNum;
FirstD = true;
}
else
OutBigNum = (Spot % 3 == 1 ? "'" : "") + BigNum.Substring(Locc, 1) + OutBigNum;
}
Spot = 0;
FirstD = false;
string OutSmallNum = "";
for (int Locc = 0; Locc < SmallNum.Length; Locc++)
{
Spot++;
if (FirstD == false)
{
OutSmallNum += SmallNum.Substring(Locc, 1);
FirstD = true;
}
else
{
OutSmallNum += (Spot % 3 == 1 ? "," : "") + SmallNum.Substring(Locc, 1);
}
if (Locc >= MaxLenPostDec - 1)
break;
if (Locc == SmallNum.Length - 1 && MinLenPostDec > Locc)
{
for (int Locc2 = Locc; Locc2 < MinLenPostDec - 1; Locc2++)
{
Spot++;
OutSmallNum += (Spot % 3 == 1 ? "," : "") + 0;
}
}
}
CustD = OutBigNum + "." + OutSmallNum;
return CustD;
}