What is the equivalent PHP function number_format in C #?

Let's say I have the following values:

var upSize = 365308443361.28;
var downSize = 351897407979.52;

var size = upSize/downSize;

In PHP, I can do this:

number_format(size, 3);

and the output will be: 1.038

How can I do the same in C # with string.Format ()?

+3
source share
2 answers

A shorter (and less flexible) alternative to Darin's code:

size.ToString("0.000")

or

size.ToString("f3")
+3
source

Yes, you can use string.Format :

var upSize = 365308443361.28;
var downSize = 351897407979.52;
var size = upSize / downSize;
string result = string.Format("{0:0.000}", size);
+6
source

All Articles