0
Dim number = 5.678
Console.WriteLine(number.ToString("#,##0.##"))

displays 5.68. Is there any number format string without rounding it?

UPDATE: desired result is 5.67

maspai
  • 397
  • 6
  • 17
  • call .ToString() without passing any argument. double x= 3.1415973; Console.WriteLine(x.ToString()) – Hakunamatata Oct 26 '15 at 02:58
  • Since you don't provide the desired result, we'll have to do some guessing. Do you want the value _truncated_ to two decimal places? The full precision available from the vague data type? Something else? – HABO Oct 26 '15 at 03:06
  • 1
    @HABO: I'm sure it's more like truncate, but using format string, not function eg. `Math.Truncate` – maspai Oct 26 '15 at 03:59
  • If you override `ToString()` and add formatting support for a dagger (`†`) to indicate where to truncate a value and a double dagger (`‡`) to indicate where to _floor_ the value (so that negative values never decrease in absolute value) ... . In short, neither [standard](https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx) nor [custom](https://msdn.microsoft.com/en-us/library/0c899ak8%28v=vs.110%29.aspx) numeric format strings support truncation. – HABO Oct 27 '15 at 15:25

3 Answers3

0
 Console.Write(Math.Truncate(number * 100) / 100);

This should work. Read more answers here

Community
  • 1
  • 1
GMB
  • 337
  • 2
  • 6
  • 21
  • I'm looking for a way using `.ToString()` function, ie. number format just like in my question. – maspai Oct 26 '15 at 09:26
0

If you always have to truncate till 2 places, you can use:

Console.WriteLine("{0:F2}", number - 0.005);

Otherwise you can change number '0.005' as per your need.

Update: If you want to treat this as string, I don't think there is any readymade solution in C#, so you might have to do some extra work like below(You can create a helper method):

const double number = 5.678; //input number
var split = number.ToString(CultureInfo.InvariantCulture).Split('.');
Console.WriteLine(split[0] + (split.Length > 1 ? "." : "") + (split.Length > 1 ? split[1].Substring(0, split[1].Length > 1 ? 2 : split[1].Length) : ""));

Input: 5.678; Output: 5.67;


Input: 5.6325; Output: 5.63;


Input: 5.64; Output: 5.64


Input: 5.6; Output: 5.6;


input: 585.6138 output: 585.61

ATP
  • 553
  • 1
  • 3
  • 16
  • CMIIW. This still rounds the number, and the workaround is subtracting the number with 0.005, right? – maspai Oct 26 '15 at 09:29
  • Yes it rounds. But it is easier solution and provides you expected output in all cases. If you must treat it like string you can refer to my updated answer. – ATP Oct 26 '15 at 09:49
  • I'm sorry maybe it's my mistake in question. What I mean about using `ToString()` is using it with string format, eg. `ToString("#,##0.##")`, not treating the number as string. – maspai Oct 27 '15 at 03:35
0

I'm really surprised that not default functionality. This is basically ATP answer except his puts commas between the thousands place.

int precision = 2;
string format = "0.##";
decimal rounder = 5.0m/(decimal) Math.Pow(10, precision + 1);
var result = (5.678m - rounder).ToString(format); // This doesn't work for a value of 0m

One line:

result = (5.678m - 5.0m / (decimal)Math.Pow(10, 2 + 1)).ToString("0.##");

Paul Totzke
  • 1,470
  • 17
  • 33