I have a requirement to format all my string to the format ##.###/##.### Example: If input value is 3/4 then i need to format it to 03.000/04.000 If my input value is 1.4/2.0 then i need to format it to 01.400/02.000 I have tried the below, which is giving the expected output. Can someone suggest is there any direct function or better way to do this.
using System;
class Test {
// Main Method
static void Main(string[] args)
{
String input = "23.3/4.0";
String[] splittedValue = input.Split('/');
Decimal numerator=Convert.ToDecimal(splittedValue[0]);
Decimal denominator=Convert.ToDecimal(splittedValue[1]);
String format = "00.000";
String test1 = numerator.ToString(format);
String test2 = denominator.ToString(format);
}
}
1.4
to01.400
. WithN3
you could add precision like1.400
but without leading zero. WithPadLeft
you could add leading zero, but now you would have two operations instead of one. \$\endgroup\$