SQLite 圆形中点

SQLite round midpoint

我正在尝试像这样使用 SQLite 函数 ROUND:

select ROUND(1.655, 2)

结果是 1.65,但我需要它四舍五入到 1.66,就像在 c# 上一样

Math.Round(1.655, 2, MidpointRounding.AwayFromZero)

有办法吗?

所以我一直在尝试使用三十二上校的技巧,使用 Binding functions,但是 sqlite-net 不提供这样的功能。 我已经转向了System.Data.SQLite.dll,所以要帮助别人:

首先在c#中创建自定义函数

[SQLiteFunction(Name = "RoundAccounting", Arguments = 2, FuncType = FunctionType.Scalar)]
public class RoundAccounting : SQLiteFunction
{
    public override object Invoke(object[] args)
    {
        decimal value = 0;
        int decimalPlaces = 0;

        if(decimal.TryParse(args[0].ToString(), out value) && int.TryParse(args[1].ToString(), out decimalPlaces))
            return Math.Round(value, decimalPlaces, MidpointRounding.AwayFromZero);

        return 0;
    }
}

然后在数据层我们将函数绑定到创建的连接:

using (SQLiteConnection connection = new SQLiteConnection(string.Format("Data Source={0}", dbPath)))
        {
            var function = new RoundAccounting();
            var attributes = function.GetType().GetCustomAttributes(typeof(SQLiteFunctionAttribute), true).Cast<SQLiteFunctionAttribute>().ToArray();
            await connection.OpenAsync();
            connection.BindFunction(attributes[0], function);

            SQLiteCommand command = new SQLiteCommand("SELECT RoundAccounting(somecolumn, 2);", connection); .....