Rounding down with a number. Finding the n in n.mp - objective-c

Bit of an odd question. I'm using cocoa.
I have a series of numbers e.g.:
0.87
0.32
1.12
2.34
8.82
12.66
and I want to get the bit before the decimal place, with no rounding.
Like this:
0.87 -> 0
0.32 -> 0
1.12 -> 1
2.34 -> 2
8.82 -> 8
12.66 -> 12
I can round the numbers with no problem, but I can't figure out how to just take the 'rounded down' figure in an elegant and non complicated way. Can you help?

use floor(double). Or cast to an int

Simply cast it to an int and you're good to go.
NSLog(#"n is %i", n);
%i will automatically cast it to int. :P

float x = 0.89;
int y = x;
int round down automatically.

Related

How can I round to next floor 0.5 in Kotlin?

I would like to round Double variables to the next lower 0.5 value.
For instance
12.03 -> 12
12.44 -> 12
12.56 -> 12.5
12.99 -> 12.5
There should be an elegant easy Kotlin like way?
Multiply by 2, take the floor, then divide by 2 again.
There doesn't seem to be a built-in way, but you can always write it yourself:
fun Double.roundDownToMultipleOf(base: Double): Double = base * floor(this / base)
Use as 12.56.roundDownToMultipleOf(0.5).

sql server update double rounding from steps

I have this issue: in my database there's a field that is a double value. I need that this value is rounded by a constant step:
for example I set a step of 20.0 and I wish that a value 17.8 become 20.0, a value of 31.2 become 40.0 etc..
how can I do this with a query?
thanks in advance
declare #stepsize float = 20.0
declare #val float = 17.6
select CEILING(#val / #stepsize) * #stepsize -- Result 20.0
set #val = 31.2
select CEILING(#val / #stepsize) * #stepsize -- Result 40.0
It's unclear from your question what 6.0 should 'round' to with a stepzie of 20.0. Should it round down to zero?
You do this by dividing the value by the step, rounding it and then multiplying by the step. Most SQL dialects have arithmetic functions.

Round Numbers Evenly

How do I round numbers up or down depending on the value in object C.. for example.
Lets say the number is 143 - I would want to round down to 140
but if the number is 146 - I would want to round up to 150
any suggestions?
Assuming 145 should round to 150 (that's the standard in science and technology), the formula is:
x_rounded = ((x + 5)/10)*10;
More generally, when rounding to the nearest n, it's
x_rounded = ((x + n/2)/n)*n;
It comes from the fact that integer division always rounds down.
For negative numbers, it's slightly more tricky.
EDIT: also assuming it's all integers. With floats/doubles, better use the C math library, as division works differently. Like this:
#include <math.h>
x_rounded = floor((x+5)/10) * 10;
Round value x to precision p, where 0 < p < infinite. (f.ex. p=0.25, 0.5, 1, 2, 3, 10,…)
float RoundTo(float x, float p)
{
float y = 1/p;
return int((x+(1/(y+y)))*y)/y;
}
float RoundUp(float x, float p)
{
float y = 1/p;
return int((x+(1/y))*y)/y;
}
float RoundDown(float x, float p)
{
float y = 1/p;
return int(x*y)/y;
}
The lround function will round a float to the nearest integer. You can fairly easily get it to round to a multiple of 10 by dividing the number by 10, rounding, then multiplying by 10.
in code:
10 * lround(x / 10.0);
I would think the simplest solution would be to include math.h and use the round() function.
For rounding floats to nearby integer values, check out the C functions floorf(), ceilf() and roundf().
For rounding integers to (say), the closest multiple of ten, the formula given by Seva should work...
This will definitely solve your worries.
- (int) roundToNearest5:(int) value
{
return (value+(5-(value%5));
}

Divide int into 2 other int

I need to divide one int into 2 other int's. the first int is not constant so one problem would be, what to do with odd numbers because I only want whole numbers. For example, if int = 5, then int(2) will = 2 and int(3) will = 3. Any help will greatly be appreciated.
Supposing you want to express x = a + b, where a and b are as close to x/2 as possible:
a = ceiling(x / 2.0);
b = floor(x / 2.0);
That's pseudo code, you have to find out the actual functions for floor and ceiling from your library. Make sure the division is performed as floating point numbers.
As pure integers:
a = x / 2 + (x % 2 == 0 ? 0 : 1);
b = x / 2
(This may be a bit fishy for negative numbers, because it'll depend on the behaviour of division and modulo for negative numbers.)
You can try ceil and floor functions from math to produce results like 2 and 3 for odd inputs;
int(2)=ceil(int/2); //will produce 3 for input 5
int(3)=floor(int/2); //will produce 2 for input 5
Well my answer is not in Objective-C but i guess you could translate this easily.
My idea is:
part1 = source_number div 2
part2 = source_number div 2 + (source_number mod 2)
This way the second number will be bigger if the starting number is an odd number.

How would I do this in a program? Math question

I'm trying to make a generic equation which converts a value. Here are some examples.
9,873,912 -> 9,900,000
125,930 -> 126,000
2,345 -> 2,400
280 -> 300
28 -> 30
In general, x -> n
Basically, I'm making a graph and I want to make values look nicer. If it's a 6 digit number or higher, there should be at least 3 zeros. If it's a 4 digit number or less, there should be at least 2 digit numbers, except if it's a 2 digit number, 1 zero is fine.
(Ignore the commas. They are just there to help read the examples). Anyways, I want to convert a value x to this new value n. What is an equation g(x) which spits out n?
It is for an objective-c program (iPhone app).
Divide, truncate and multiply.
10**x * int(n / 10**(x-d))
What is "x"? In your examples it's about int(log10(n))-1.
What is "d"? That's the number of significant digits. 2 or 3.
Ahhh rounding is a bit awkward in programming in general. What I would suggest is dividing by the power of ten, int cast and multiplying back. Not remarkably efficient but it will work. There may be a library that can do this in Objective-C but that I do not know.
if ( x is > 99999 ) {
x = ((int)x / 1000) * 1000;
}
else if ( x > 999 ) {
x = ((int) x / 100) * 100;
}
else if ( x > 9 ) {
x = ((int) x / 10) * 10;
}
Use standard C functions like round() or roundf()... try man round at a command line, there are several different options depending on the data type. You'll probably want to scale the values first by dividing by an appropriate number and then multiplying the result by the same number, something like:
int roundedValue = round(someNumber/scalingFactor) * scalingFactor;