Please how do I convert a string from 2 decimal places to 5 decimal places in c#?
For example; I'm trying to convert 12.14 to 12.14000 using C#
Please how do I convert a string from 2 decimal places to 5 decimal places in c#?
For example; I'm trying to convert 12.14 to 12.14000 using C#
What you're looking for is a ToString()
argument:
12.14.ToString("#.00000")
-> "12.14000"
Look at PadRight
method of String
class. MSDN description.
Exaple:
using System;
public class Program
{
static public void Main()
{
string a = "12.14";
string newA = a.PadRight(8, '0');
Console.Write(newA);
}
}
Result:
12.14000
This code always fill to 5 places after point:
using System;
public class Program
{
static public void Main()
{
string a = "332.143";
int padLen = (5 - (a.Length - a.IndexOf('.') - 1)) + a.Length;
string newA = a.PadRight(padLen, '0');
Console.WriteLine(newA);
}
}
Result:
332.14300