Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios round number to int or .5

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!

like image 320
manuelBetancurt Avatar asked Sep 07 '12 06:09

manuelBetancurt


People also ask

Does INT round up or down Swift?

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.

How do you round an int in Swift?

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.

How do you round a number in Objective C?

Use lroundf() to round a float to integer and then convert the integer to a string.


2 Answers

Just do this:

x = round(x * 2.0) / 2.0;

This rounds x to the nearest multiple of 0.5.

like image 64
Paul R Avatar answered Oct 16 '22 19:10

Paul R


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
like image 36
Jay Mayu Avatar answered Oct 16 '22 20:10

Jay Mayu