C# decimal Places

In C# i have value like

0.1295
18.3282
2423.328432

The output I need is

0.12
18.32
2423.32

I mean, I need to take two digits after decimal WITHOUT ROUNDING OFF

Who is Participating?

Commented:
sorry it should be:-

`````` Math.Truncate(2423.328432 * 100) / 100;
``````
0

Commented:
Try the following: and for rest also

double d = 2423.328432;
d = ( (double) ( (int) (d * 1000.0) ) ) / 1000.0;
0

Author Commented:
Not working

If i pass 0.435 i get output same

it should be 0.43 intstead !
0

Commented:
you may use:-

`````` Math.Truncate(2423.328432 * 1000) / 1000;
``````

0

Author Commented:
@ same as ID: 35746329
NO CHANGE IN OUTPUT
0

Commented:
Use the following:

private void button4_Click(object sender, EventArgs e)
{
decimal  a  = 0.1295M;
decimal  b = 18.3282M;
decimal  c = 2423.328432M;
MessageBox.Show(FormatDecimal(a));
MessageBox.Show(FormatDecimal(b));
MessageBox.Show(FormatDecimal(c));

}

private string FormatDecimal(decimal val)
{
decimal newValue = ((Int64)(val * 100)) / 100m;
return newValue.ToString("N2");
}
http://ecamilingonline.blogspot.com/2010/11/return-2-decimal-places-without.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.