Solved

# C# decimal Places

Posted on 2011-05-12
Medium Priority
438 Views
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

0
Question by:GRChandrashekar

LVL 9

Expert Comment

ID: 35746329
Try the following: and for rest also

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

Author Comment

ID: 35746381
Not working

If i pass 0.435 i get output same

it should be 0.43 intstead !
0

LVL 9

Expert Comment

ID: 35746384
you may use:-

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

0

Author Comment

ID: 35746396
@ same as ID: 35746329
NO CHANGE IN OUTPUT
0

LVL 9

Accepted Solution

mayank_joshi earned 2000 total points
ID: 35746418
sorry it should be:-

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

LVL 22

Expert Comment

ID: 35746434
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
###### Suggested Courses
Course of the Month14 days, 9 hours left to enroll