in Javascript, the toFixed() method formats a number to use a specified number of trailing decimals. Here is toFixed method in javascript
How can i write a same method in c#?
in Javascript, the toFixed() method formats a number to use a specified number of trailing decimals. Here is toFixed method in javascript
How can i write a same method in c#?
Use the various String.Format() patterns.
For example:
int someNumber = 20;
string strNumber = someNumber.ToString("N2");
Would produce 20.00. (2 decimal places because N2 was specified).
Standard Numeric Format Strings gives a lot of information on the various format strings for numbers, along with some examples.
You could make an extension method like this:
using System;
namespace toFixedExample
{
public static class MyExtensionMethods
{
public static string toFixed(this double number, uint decimals)
{
return number.ToString("N" + decimals);
}
}
class Program
{
static void Main(string[] args)
{
double d = 465.974;
var a = d.toFixed(2);
var b = d.toFixed(4);
var c = d.toFixed(10);
}
}
}
will result in: a: "465.97", b: "465.9740", c: "465.9740000000"