TamHoang
TamHoang

Reputation: 133

C# Convert decimal to string with specify format

I need to convert decimal number a to string b folowing:

How can I do that with 1 command?

(Same question with 1)

Upvotes: 13

Views: 36394

Answers (5)

Stephen Zeng
Stephen Zeng

Reputation: 2818

For the first one, if you don't know how many the digits the variable could be, you can have a extension method:

public static string ToSpecificFormat(this decimal value)
{
    var count = BitConverter.GetBytes(decimal.GetBits(value)[3])[2];
    return value.ToString(count == 0 ? "N1" : "N" + count);
}

This will make sure there is at least 1 digit in the output.

For the second one, the selected answer will fail if the number > 1000000000. This one should work:

public static string ToFixedLength(this decimal value)
{
    if (value >= 1000000000m) return value.ToString("N0");
    
    var format = 9 - Math.Floor(value).ToString().Length;
    return value.ToString("N" + format);
}

output:

1.234m.ToFixedLength(); // 1.23400000
101m.ToFixedLength(); // 101.000000
123456789123m.ToFixedLength(); // 123,456,789,123

Upvotes: 0

active92
active92

Reputation: 654

I manage to do it using double. Is this what you need? I don't quite get the second part of your question.

double a = 12;
string b = a.ToString("0.0000000000######");
Console.WriteLine(b);

Upvotes: 1

Kalu Singh Rao
Kalu Singh Rao

Reputation: 1697

if you want the result as a string, just parse it and format it to one decimal places:

string strTemp = 12;
double temp = Double.Parse(strTemp, CultureInfo.InvariantCulture);
string result = temp.ToString("N1", CultureInfo.InvariantCulture);

Round off to 2 decimal places eg. 27.658 => 27.66

Ensure that there are always 2 decimal places eg. 12 => 12.00, 12.5 => 12.50

Good fit for currency amounts.

strTemp.ToString("F");

Upvotes: 1

Gilad Green
Gilad Green

Reputation: 37299

decimal a = 12;
var b = a.ToString("N1"); // 12.0

a = 1.2m;
b = a.ToString(); // 1.2

a = 101m;
b = a.ToString("N10"); // 101.0000000000

a = 1.234m;
b = a.ToString("N10"); // 1.2340000000

For the second part of your question - where you want a total length of 10 then:

decimal a = 1.234567891m;
int numberOfDigits = ((int)a).ToString().Length;
var b = a.ToString($"N{9 - numberOfDigits}"); //1.23456789

//Or before C# 6.0
var b = a.ToString("N" + (9 - numberOfDigits)); //1.23456789

Basically ((int)number).ToString().Length gives you the amount of digits before the . (converting to int will remove the fractions) and then reducing that from the number of digits after the . (and also -1 for the decimal point itself)

Upvotes: 14

sujith karivelil
sujith karivelil

Reputation: 29006

You can use .ToString() to do this task:

decimal aDecimalVal = 10.234m;
string decimalString = aDecimalVal.ToString("#.000"); // "10.234"
aDecimalVal.ToString("#.00"); // "10.23"
aDecimalVal.ToString("#.0000"); // "10.2340"

The number of 0 after the . in the format string will decide the number of places in the decimal string.

Updates: So you want to find the number of digits after the decimal points, So the changes in the code will be like the following:

decimal aDecimalVal = 10.2343455m;
int count = BitConverter.GetBytes(decimal.GetBits(aDecimalVal)[3])[2];
string formatString = String.Format("N{0}",count.ToString());
string decimalString = aDecimalVal.ToString(formatString); // "10.2343455"

Upvotes: 4

Related Questions