Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting to double to two decimal places

Hi i am a c# novice would someone politely tell me how to convert the values of this piece of code to a double/rounded decimal.. thanks in advance

DataTable dtValues = new DataTable("GetValues");

strValueNumber = ValueNumber[0].ToString();
dtGetValues = SQLMethods.GetValues(strValueNumber);

total = 0;

for (int i = 0; i < dtValues.Rows.Count; i++)
{
    total1 = total1 + Convert.ToInt32(dtGetValues.Rows[i]["total_1"]);                  
    total2 = total2 + Convert.ToDouble(dtGetValues.Rows[i]["total_2l"]) * .45; 

    tbtotal1.Text = total1.ToString();
    tbtotal2.Text = total2.ToString(); 
}
}               
catch (Exception ex)
{
    MessageBox.Show("Error in returning selected Values. " +
                    "Processed with error:" + ex.Message);
}
}
like image 811
user1483145 Avatar asked Jun 26 '12 17:06

user1483145


People also ask

How do I change formula to 2 decimal places in Excel?

By using a button: Select the cells that you want to format. On the Home tab, click Increase Decimal or Decrease Decimal to show more or fewer digits after the decimal point.

What is an example of 2 decimal places?

4.732 rounded to 2 decimal places would be 4.73 (because it is the nearest number to 2 decimal places). 4.737 rounded to 2 decimal places would be 4.74 (because it would be closer to 4.74).

How do you convert a double to two decimal places in Swift?

By using round(_:) , ceil(_:) , and floor(_:) you can round Double and Float values to any number of decimal places in Swift.


1 Answers

My answer is pretty late but for those out there like me who want:

to convert to double/decimal and also want the value to always show 2 decimal places (.00) as String

tbtotal2.Text = Math.Round(total2, 2).ToString("#.00"); 

The below means two decimal places at all times.

"#.00"

The below means two decimal places if there is value.

"#.##"
like image 80
alexan Avatar answered Oct 25 '22 23:10

alexan