Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am importing data from Excel to SQL Server database.

how to convert a float value in excel to round up value in SQl Query. i need to convert here the Basic Salary from float value into Round Up value(when passing to database).

C#
string ins = "insert into PaySalary1(BasicSalary,HRA)values
('" + Convert.ToDecimal(dtExcel.Rows[i][0]) + "','" + dtExcel.Rows[i][1] + "')";
Posted
Updated 29-Jun-13 2:48am
v2
Comments
Maciej Los 29-Jun-13 8:49am    
Why are you trying to insert float data as a text?

Hello,

C#
double RoundUp(double value)
{
    return Math.Ceiling(value);
}

void Main()
{
    float Salary1 = 1.234F;
    float Salary2 = 1.567F;
    float Salary3 = 1.987F;
    float Salary4 = 1.111F;

    Console.WriteLine(RoundUp(Salary1)); //Output: 2
    Console.WriteLine(RoundUp(Salary2)); //Output: 2
    Console.WriteLine(RoundUp(Salary3)); //Output: 2
    Console.WriteLine(RoundUp(Salary4)); //Output: 2
}


JAFC
 
Share this answer
 
PLease, read my comment and have a look here: ROUND(T-SQL)[^]
 
Share this answer
 
Typically, rounding of salary amounts uses the same algorithm as is implemented in Math.Round. The example below uses Math.Round to round data retrieved from Excel before using it in the INSERT statement. The example uses the ToString() method with a format to convert to a text string for use in the INSERT statement. Note that the value is not enclosed in apostrophes because the BasicSalary column in the database should be defined as a Numeric, Decimal or Money data type.

The example below rounds to two decimal places which is common for salaries.

C#
string ins = "insert into PaySalary1(BasicSalary,HRA) values (" + Math.Round(dtExcel.Rows[i][0],2).ToString("###########0.00") + ",'" + dtExcel.Rows[i][1] + "')";
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900