I’m looking to make a customer decimal format. What I get right now if I do “(3003.3003d).ToString(#,###.00##”) is “3,003.3003”. What I am trying to get as a format would be “3’003.300,3”.
I can use a customer method, math.floor and break it apart myself, but I’m curious if there is a way to use culture info to do what I am looking for. Anyone know of a way to do that?
Wrote the guy with custom IFormatprovider as the answer as the tutorial was pretty good, but I do not believe a current culture exists with what I needed. Here was my end answer :
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;
}
You should be looking into the overload
ToString("#,###.00##", myFormatProvider)So you need a custom IFormatprovider.