Reference > Functions > Mathematical

Round

  • 2005, 2008, 2012, 2014, 2016, 2017, 2019, 2022

Round Mathematical Function

Use the Round function to return a numeric value rounded to the specified length or precision.

Syntax

db.fx.Round({expression}, {length} [,{function}])

Arguments

expression
The value to round.
length
Precision to round `expression`.
function
The type of operation to perform.

Returns

The same type as `expression`.

Syntax

db.fx.Round({expression}, {length} [,{function}])

Arguments

  • {expression} - Any dbExpression element of a numeric data type.
  • {length} - Precision to round {expression}, must be one of:
    • short
    • int
    • long
    • AnyElement<short>
    • AnyElement<int>
    • AnyElement<long>
  • {function} - The type of operation to perform, must be one of:
    • short
    • int
    • long
    • AnyElement<short>
    • AnyElement<int>
    • AnyElement<long>

Returns

expression typereturn type
bytebyte
byte?byte?
AnyElement<byte>byte
AnyElement<byte?>byte?
decimaldecimal
decimal?decimal?
AnyElement<decimal>decimal
AnyElement<decimal?>decimal?
doubledouble
double?double?
AnyElement<double>double
AnyElement<double?>double?
floatfloat
float?float?
AnyElement<float>float
AnyElement<float?>float?
intint
int?int?
AnyElement<int>int
AnyElement<int?>int?
longlong
long?long?
AnyElement<long>long
AnyElement<long?>long?
shortshort
short?short?
AnyElement<short>short
AnyElement<short?>short?

Examples

Select Statement

Round of total purchase amount for all purchases to the nearest dollar.

IEnumerable<double> value = db.SelectMany(
        db.fx.Round(dbo.Purchase.TotalPurchaseAmount, 0)
    )
    .From(dbo.Purchase)
    .Execute();

Where Clause

Select all purchase ids where the rounded value is equal to 100.

IEnumerable<int> value = db.SelectMany(
        dbo.Purchase.Id
    )
    .From(dbo.Purchase)
    .Where(db.fx.Round(dbo.Purchase.TotalPurchaseAmount, -1) == 100)
    .Execute();

Order By Clause

Select the floor value of total purchase amount for all purchases ordered by the floor value descending.

IEnumerable<double> value = db.SelectMany(
        dbo.Purchase.TotalPurchaseAmount
    )
    .From(dbo.Purchase)
    .OrderBy(db.fx.Round(dbo.Purchase.TotalPurchaseAmount, 1).Desc())
    .Execute();

Group By Clause

Select the payment method and floor of total purchase amount for all purchases, grouped by payment method type and ordered by the floor of total purchase amount.

IEnumerable<dynamic> values = db.SelectMany(
        dbo.Purchase.PaymentMethodType,
        db.fx.Round(dbo.Purchase.TotalPurchaseAmount, 2).As("TotalPurchaseAmount")
    )
    .From(dbo.Purchase)
    .GroupBy(
        dbo.Purchase.PaymentMethodType,
        db.fx.Round(dbo.Purchase.TotalPurchaseAmount, 2)
    )
    .Execute();

Having Clause

Select the payment method and absolute value of total purchase amount for all purchases, grouped by payment method type having an absolute value greater than 10 and ordered by the absolute value of total purchase amount.

IEnumerable<dynamic> values = db.SelectMany(
        dbo.Purchase.PaymentMethodType,
        dbo.Purchase.TotalPurchaseAmount
    )
    .From(dbo.Purchase)
    .GroupBy(
        dbo.Purchase.PaymentMethodType,
        dbo.Purchase.TotalPurchaseAmount
    )
    .Having(db.fx.Round(dbo.Purchase.TotalPurchaseAmount, 2) > 10)
    .Execute();
Previous
Rand

© 2024 dbExpression. All rights reserved.