13

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

  • b must be haven '.' character. Eg:
    • a = 12 -> b = "12.0"
    • a = 1.2 -> b = "1.2"
    • a = 1.234 -> b = "1.234"

How can I do that with 1 command?

  • b must be haven exactly 10 character. Eg:
    • a = 101 -> b = "101.000000"
    • a = 1.234 -> b = "1.23400000"
    • a = 1.234567891 -> b = "1.23456789"

(Same question with 1)

0

5 Answers 5

14
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)

Sign up to request clarification or add additional context in comments.

3 Comments

In 2nd question, total characters of b is 10, not number of digits after '.'
For the 2nd one, it needs to consider when the total length > 10. You probably need an extension method for the 2nd one.
@GiladGreen what about decimal a = 12345678910m?
4

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"

1 Comment

but I don't know exact the number of digits after '.' character
1

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");

Comments

1

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);

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.