c# - How do i make that it will show me the file size with only two digits after the dot(point)? -
c# - How do i make that it will show me the file size with only two digits after the dot(point)? -
public static string formatsize(double size) { const long bytesinkilobytes = 1024; const long bytesinmegabytes = bytesinkilobytes * 1024; const long bytesingigabytes = bytesinmegabytes * 1024; const long bytesinterabytes = bytesingigabytes * 1024; tuple<double, string> unit; if (size < bytesinterabytes) if (size < bytesingigabytes) if (size < bytesinmegabytes) if (size < bytesinkilobytes) unit = tuple.create(size, "b"); else unit = tuple.create(size / bytesinkilobytes, "kb"); else unit = tuple.create(size / bytesinmegabytes, "mb"); else unit = tuple.create(size / bytesingigabytes, "gb"); else unit = tuple.create(size, "tb"); homecoming string.format("{0} {1}",unit.item1, unit.item2); }
in case see kb , is: 116.1234567890 kb im getting 10 numbers after point. how can create give 2 digits after point ?
just utilize of standard .net formatting literals. numeric value 2 digits after decimal point, utilize {0:n2}
:
homecoming string.format("{0:n2} {1}", unit.item1, unit.item2);
this should give you:
116.12 kb
for more info, see msdn documentation on standard numeric format strings.
c# .net winforms
Comments
Post a Comment