This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using String Format to show decimal upto 2 places or simple integer
How to set decimal point in 2 decimal places?
I have Price field in my view. it have the following values 2.5 and 44.
I want to display this value to 2.50 and 44.00 i use the following code
#{decimal prolistprice = decimal.Parse(item.OtherFields["Price"].ToString());}
$#Math.Round(prolistprice, 2, MidpointRounding.AwayFromZero)
in which item.OtherFields["price"] is a object i convert it to string and then decimal
but Math.round is not working it shows 2.5 and 44 only..
Can anyone help this
Math.Round does just that - round.
To format the number you may use .ToString(formatString) like so:
item.OtherFields["price"].ToString("0.00")
Use string format function
1. string.Format("{0:n2}", 200000000.8776);
2. string.Format("{0:n3}", 200000000.8776);
3. string.Format("{0:n2}", 0.3);
/* OUTOUT
1. 200,000,000.88
2. 200,000,000.878
3. 0.30
*/
This should work for you
yourvalue.ToString ("0.00");
decimal dValue = 2.5;
string sDisplayValue = dValue.ToString("0.00");
Related
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 5 years ago.
Look at this situation:
var number1 = Math.Floor(1.9999999999999998d); // the result is 1
var number2 = Math.Floor(1.9999999999999999d); // the result is 2
In a both cases, the result should be 1. I know it's a very unlikely scenario, but possible to occur. The same ocurr with Math.Truncate method and (int) cast.
Why does it happen?
There is no exact double representation for a lot of numbers.
The double number the nearest from 1.9999999999999999 is 2, so the compiler rounds it up.
Try to print it before using your Math.Floor function !
However, the nearest from 1.9999999999999998 is still 1.something, so Floor gives out 1 .
Again, it would be enough to print the number before the function Floorto see that they were actually not anymore the one entered in the code.
EDIT : To print out the number with most precision :
double a1 = 1.9999999999999998;
Console.WriteLine(a1.ToString("G17"));
// output : 1.9999999999999998
double a2 = 1.9999999999999999;
Console.WriteLine(a2.ToString("G17"));
// output : 2
Since double precision is not always precise to 17 significative digits (including the first one before the decimal point), default ToString() will round it up to 16 significant digits, thus, in this case, rounding it up to 2 as well, but only at runtime, not at compile time.
If you put literals values into another variables, then you see it why:
var a1 = 1.9999999999999998d; // a1 = 1.9999999999999998d
var number1 = Math.Floor(a1);
Console.WriteLine(number1); // 1
var a2 = 1.9999999999999999d; // a2 = 2
var number2 = Math.Floor(a2);
Console.WriteLine(number2); // 2
As for why - this has to be something to do with precision of double and decision of compiler as to what value to use for a given literal.
This question already has answers here:
Using String Format to show decimal up to 2 places or simple integer
(18 answers)
Closed 5 years ago.
I am wanting my label to show what the total price of a product is after discount. My label does show the total price after the discount but I can't figure out how to add 2 decimal places to the final price. For example if the total price after a discount is $90 I would like my label to show $90.00. I can only figure out how to display the price without decimals.
lblTotalPrice.Text = "$" + Convert.ToString(totalPrice);
lblTotalPrice.Text = "$" + totalPrice.ToString("0.##");
This question already has answers here:
Always return positive value
(9 answers)
Closed 5 years ago.
I am calculating the difference between two numbers. If the calculation ends up being 5 - 10, it equals to "-5". If this is the case I need results to display/equal to "+5" , with the "+" sign.
I basically need reverse. So same if 10 - 5 quals to "5" I need it to display as "+5"
Code below I am using:
float rowresults = ROW1 - ROW2;
Textbox.text = rowresults.ToString();
Math.Abs is what you are looking for:
float rowresults = Math.Abs(ROW1 - ROW2);
And to add the "+"-sign to the front of the text (without changing your elsewise existing behaviour):
Textbox.text = "+" + rowresults.ToString();
This question already has answers here:
How do you round a number to two decimal places in C#?
(15 answers)
Closed 8 years ago.
I want to get the round off value of a decimal number Suppose I am getting 24.86 than i want to get 25 as final value
Look at Math.Round(decimal) and the overload which accepts a MidpointRounding argument.
Simply
Math.Round(24.86)
This will round you value to 25.
Your own logic will be
decimal d = 1.5m;
decimal r = d - Math.Truncate(d);
if (r > 0)
r = 1 - r;
decimal value = d + r;
I'm trying to find the best way to display a double in C# as follows:
7.345 should display as "73"
100.0 should display as "100"
0.234 shoud display as "02"
The input is a value between 0.00 and 10.00. I need to convert it to a filename. E.g. in case of a value of 5.4234, I should display "img54.jpg".
The problem is that I can't figure out how to display zero values in ToString() of doubles.
I tried this:
(10 * 7.345).ToString("##.") => correct
(10 * 10.00).ToString("##.") => correct
(10 * 0.000).ToString("##.") => FAIL, doesn't display anything.
(10 * 0.000).ToString("D2") => FAIL, D is not allowed in doubles
I can of course do some sophisticated string building, but if it's possible to use ToString, that would be much better of course.
Anyone an idea?
What do you want 0.00 to display as? "00"?
In that case you can try with format ToString("00.") instead.
Can you simply check if the double is 0? and If it is, set img00.jpg to your filename. That seems a lot easier than reworking your algorithm.
Why don't you convert it to an int?
int result = (int)(input * 10.00);
return result.ToString();
You could just convert to int before formatting like this:
((int)(10 * 7.345)).ToString("D2")
If you always have the input number of this format: #.###
You can multiply it by 1000 and divided by 100 and cast the result to an integer.
7.345 * 1000 = 7345 / 100 = 73.45 => Convert.ToInt32 => 73
0.000 * 1000 = 0 / 100 = 0 => Convert.ToInt32 = 0
Or you can multiply by 10 and convert to Integer.
return ((int)(input * 10.00)).ToString().SubString(0, 2);
double val = 7.345;
string result = val.ToString("0.#").Replace(".","");