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

GRChandrashekarAsked:
Who is Participating?
 
mayank_joshiCommented:
sorry it should be:-

 Math.Truncate(2423.328432 * 100) / 100;

Open in new window

0
 
s_chilkuryCommented:
Try the following: and for rest also

double d = 2423.328432;
d = ( (double) ( (int) (d * 1000.0) ) ) / 1000.0;
0
 
GRChandrashekarAuthor Commented:
Not working

If i pass 0.435 i get output same

it should be 0.43 intstead !
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
mayank_joshiCommented:
you may use:-


 Math.Truncate(2423.328432 * 1000) / 1000;

Open in new window


0
 
GRChandrashekarAuthor Commented:
@ same as ID: 35746329
NO CHANGE IN OUTPUT
0
 
Om PrakashCommented:
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.

All Courses

From novice to tech pro — start learning today.