Samarth Agarwal
Samarth Agarwal

Reputation: 2134

Double getting converted to Int WPF C#

I am working on a WPF Application using C#. My situation is such that I need the absoluted value of a double and then display it in the a label, for ex, if I have

double d=-17.00;

myLbl.Content=Math.Abs(d); OR

myLbl.Content=Math.Abs(d).ToString();

This should simply set my label to 17.00, but no! It instead sets my label to 17. I need to keep the precision upto 2 decimal places, in all cases.

What can I do? please help.

Upvotes: 0

Views: 552

Answers (2)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

string value = Math.Abs(-17.00).ToString("0.00");

//value: "17.00"

Upvotes: 1

Kenneth
Kenneth

Reputation: 28737

You need to format the output string:

myLbl.Content=Math.Abs(d).ToString("0.##%");

Upvotes: 1

Related Questions