user2099420
user2099420

Reputation:

How can I round up to the next multiple of 0.05 using SQL Server?

How can I round a floating-point number to the hundredth place, while also rounding up to the nearest multiple of 0.05 in SQL Server.

Example 4.93 would round to 4.95

Upvotes: 2

Views: 2966

Answers (2)

Andomar
Andomar

Reputation: 238086

You can use this formula, where @round_to is the number to a multiple of which you'd like to round. @round_updown chooses up or down rounding: set it to 0 to round down, to @round_to - 0.000001 to round up, or to @round_to / 2 to use middle rounding.

select  @round_to*cast((@value+@round_updown)/@round_to as int)

For example:

select  0.05*cast((4.93+0.025)/0.05 as int)

Upvotes: 7

alex
alex

Reputation: 12654

Multiply it by 20, then round it to next integer, then divide by 20.

Upvotes: 9

Related Questions