?
Solved

C# decimal Places

Posted on 2011-05-12
6
Medium Priority
?
438 Views
Last Modified: 2012-05-11
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
Comment
Question by:GRChandrashekar
6 Comments
 
LVL 9

Expert Comment

by:s_chilkury
ID: 35746329
Try the following: and for rest also

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

Author Comment

by:GRChandrashekar
ID: 35746381
Not working

If i pass 0.435 i get output same

it should be 0.43 intstead !
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35746384
you may use:-


 Math.Truncate(2423.328432 * 1000) / 1000;

Open in new window


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:GRChandrashekar
ID: 35746396
@ same as ID: 35746329
NO CHANGE IN OUTPUT
0
 
LVL 9

Accepted Solution

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

 Math.Truncate(2423.328432 * 100) / 100;

Open in new window

0
 
LVL 22

Expert Comment

by:Om Prakash
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question