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!
round() always rounds up when the decimal place is >= . 5 and down when it's < . 5 (standard rounding). You can use floor() to force rounding down, and ceil() to force rounding up.
Rounding Numbers in Swift By using round(_:) , ceil(_:) , and floor(_:) you can round Double and Float values to any number of decimal places in Swift.
Use lroundf() to round a float to integer and then convert the integer to a string.
Just do this:
x = round(x * 2.0) / 2.0;
This rounds x
to the nearest multiple of 0.5.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With