如何将Excel Sheet float值舍入到Sql Server Query中 [英] How to Round up Excel Sheet float value into Sql Server Query

查看:85
本文介绍了如何将Excel Sheet float值舍入到Sql Server Query中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据从Excel导入SQL Server数据库。



如何将excel中的浮点值转换为SQl查询中的向上舍入值。我需要将基本工资从浮动值转换为Round Up值(传递给数据库时)。



  string  ins =  插入PaySalary1(BasicSalary,HRA)值
('
+ Convert.ToDecimal(dtExcel.Rows [i] [ 0 ])+ ',' + dtExcel.Rows [i] [ 1 ] + ' );

解决方案

您好,



< pre lang =cs> 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)); // 输出:2
Console.WriteLine(RoundUp(Salary2)); // 输出:2
Console.WriteLine(RoundUp(Salary3)); // 输出:2
Console.WriteLine(RoundUp(Salary4)); // 输出:2
}





JAFC


请阅读我的评论,看看这里: ROUND(T-SQL) [ ^ ]


通常,工资金额的舍入使用与 Math.Round中实现的算法相同的算法。下面的示例使用 Math.Round 来舍入从Excel检索的数据,然后在 INSERT 语句中使用它。该示例使用 ToString()方法,其格式转换为文本字符串,以便在 INSERT 语句中使用。请注意,撇号中的值不是,因为数据库中的BasicSalary列应定义为数字十进制 Money 数据类型。



以下示例四舍五入到工资常见的两位小数。



  string  ins =  插入PaySalary1( BasicSalary,HRA)值( + Math.Round(dtExcel.Rows [i] [ 0 ], 2 )。ToString(  ########### 0.00)+  ,' + dtExcel.Rows [i] [ 1 ] +  '); 


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).

string ins = "insert into PaySalary1(BasicSalary,HRA)values
('" + Convert.ToDecimal(dtExcel.Rows[i][0]) + "','" + dtExcel.Rows[i][1] + "')";

解决方案

Hello,

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


PLease, read my comment and have a look here: ROUND(T-SQL)[^]


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.

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


这篇关于如何将Excel Sheet float值舍入到Sql Server Query中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆