8

i have a list of numbers in a big long list imported from a CSV,

I need to consult the list after the user enters a number in a textfield, so after the uses enters the number I need to take the number to the nearest integer or to x.5

for example

1;
1.5;
2;

and so on

so if the user enters 1.2, it will go to 1, and if user enters 1.45 goes to 1.5

so that is the general rule, but for a long set of numbers,

so how can i accomplish this?

thanks a lot!

4

2 回答 2

33

Just do this:

x = round(x * 2.0) / 2.0;

This rounds x to the nearest multiple of 0.5.

于 2012-09-07T06:41:14.557 回答
1

it'd look like something below in Swift

var valueToBechanged  = 3.45
// Casting the valueToBechanged to Double because it can be Float, Int etc
var roundedRating : Double = round(Double(valueToBechanged) * 2) / 2.0
于 2015-07-15T09:49:54.550 回答