Reputation: 33
I want my output is not rounded up whenever I put values in my TextBox. For example 5000/2000 the output I want to display is 2 but It displays 3.
Here is my Code
private void InsertReceipt()
{
decimal Stub;
Stub = decimal.Parse(txtAmount.Text) / 2000;
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Ticket(CustomerID, Date, Store, Amount, NoStub)" + "VALUES (@CustomerID, @Date, @Store, @Amount, @NoStub) ";
cmd.Parameters.AddWithValue("@CustomerID", txtCustomerID.Text);
cmd.Parameters.AddWithValue("@Date", dtpDate.Value.Date.ToString());
cmd.Parameters.AddWithValue("@Store", txtStore.Text);
decimal amount = decimal.Parse(txtAmount.Text);
cmd.Parameters.AddWithValue("@Amount", amount);
cmd.Parameters.Add("@NoStub", SqlDbType.Decimal).Value = Stub;
cmd.ExecuteNonQuery();
}
Upvotes: 0
Views: 378
Reputation: 10030
I guess you want Stub value in decimal only but don't want rounded
For this you can use Use Math.Floor
which returns the largest integer less than or equal to the specified number.
Stub = Math.Floor(decimal.Parse(txtAmount.Text) / 2000);
Upvotes: 0
Reputation: 86
I think Math.Floor is what you're searching for:
Stub = Math.Floor(decimal.Parse(txtAmount.Text) / 2000);
Floor() always rounds down.
Upvotes: 6
Reputation: 204
if you want your output to be integer then why you use decimal data type???
int stud = 5000/2000;
you will get your output directly in Integer (rounded)
Upvotes: 0
Reputation: 3562
If you divide 2 numbers and assign to an integer, the result will be truncated (which is what you want):
decimal d = 5000 / 2000;
int i = (int)d;
Console.WriteLine(i);
Upvotes: 0