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.##");
Related
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();
How to convert a decimal number (e.g. 2.5) to year and month (2 years and 6 months) and add it to a given date? I tried DateTime.TryParse and it didn't work.
If you are using it for years then multiply the float you have by 12. 2.5 becomes 30months. Then use the addmonths function. If I enter 5 then it will add 60 months which is 5 years
Usually you could just add a TimeSpan or use one of the Add methods, like this:
decimal yearsToAdd = (decimal)2.5;
int years = (int)Math.Floor(yearsToAdd);
decimal months = yearsToAdd - years;
int actualMonths = (int) Math.Floor(months * 12); // or Ceiling or Round
DateTime x = DateTime.Now.AddYears(years).AddMonths(actualMonths);
The problem is, that when you decimal doesn't yield an exacat number of months, how would you know how long e.g. half a month is?
28.0 / 2, 29.0 / 2, 30.0 / 2 or 31.0 / 2?
Would you take the length of the month you started with or one of the possible two months you end up with?
If you init date is dt than
dt = dt.AddMonths((int)(2.5*12));
decimal x =(decimal)2.5;
int nbYear = Convert.ToInt16(x);
var y = x - Math.Truncate(x);
int nbMonth =Convert.ToInt16 (y*12);
// MessageBox .Show (string.Format (" {0} years and {1} months ",nbYear ,nbMonth ));
DateTime dat=DateTime .Now ; // or given date
DateTime dat2 = dat.AddYears(nbYear).AddMonths(nbMonth);
If month is your smallest unit then the solution is, as pointed by many, to multiply number by 12. A more accurate alternative would be to use ticks.
decimal years=3.14592M; // No idea where this came from.
long ticks = (long)(356.0M * (decimal)TimeSpan.TicksPerDay * years);
DateTime futureDate=DateTime.Today.AddTicks(ticks);
Note that solution will not compensate for leap years. It is not difficult to extend it - you need to calculate number of leap years in the period and use average instead of 356.0M to calculate ticks per year (i.e. avg. number of days per year * ticks per day).
This question already has answers here:
How do you round a number to two decimal places in C#?
(15 answers)
Closed 6 years ago.
I am measuring serverPerformance.
Right now I get this in milliseconds but I want to convert this into seconds when it is more or equal to 500 milliseconds.
I Accomplish this like this
public static string ConvertMillisecondsToSeconds(long milliseconds)
{
if(milliseconds >= 500)
return Math.Ceiling(TimeSpan.FromMilliseconds(milliseconds).TotalSeconds).ToString() + "s";
return milliseconds.ToString() + "ms";
}
My problem
When I return totalseconds without Math.ceiling I get for example:
0,846 seconds
When I use math.Ceiling method I get 1 second.
Desiered result
0,8 seconds.
basically im searching for a method that will return a decimal value, with 2 decimals.
var milliseconds = 0.846;
milliseconds.ToString("0.00"); // Gives 0.85
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;
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");