Agenda: I want to round up the whole numbers (Number) in the following format,
If (LastDigitOfNumber=0)
{
Result=Number;
}
Else If (1<=LastDigitOfNumber<=3)
{
Result=Round down to nearest 10;
}
Else If (4<=LastDigitOfNumber<=6)
{
Result= Number as a multiple of 5;
// for ex: if the number is 34, result=35
// if number is 36, result=35
}
Else If (7<=LastDigitOfNumber<=9)
{
Result= Number rounded up to the nearest multiple of 10;
}
Question: I am not sure how to achieve this with excel, if else function or rounding function in excel doesn't seem to work. Can anybody please help?
Thanks!
Try this formula:
=IF(MOD(A1,10)=0,A1,
IF(AND(MOD(A1,10)>=1, MOD(A1,10)<=3), FLOOR(A1/10,1)*10,
IF(AND(MOD(A1,10)>=4, MOD(A1,10)<=6), FLOOR(A1/10,1)*10+5,
IF(AND(MOD(A1,10)>=7, MOD(A1,10)<=9), CEILING(A1/10,1)*10,"X")
)
)
)
Results
A1=10 --> 10
A1=11 --> 10
A1=12 --> 10
A1=13 --> 10
A1=14 --> 15
A1=15 --> 15
A1=16 --> 15
A1=17 --> 20
A1=18 --> 20
A1=19 --> 20
A1=20 --> 20
This works perfect for the above posted question,
=IF(MOD(E5,10)=0,E5,E5+CHOOSE(MOD(E5,10),-1,-2,-3,1,0,-1,3,2,1,0))
E5 is the cell number in the spreadsheet.
Related
First of all, this is my first week trying out C# or any other programming language for that matter, also my first post here on Stackoverflow!
Been working on this change calculator for a while now, trying to get it to round the result to either 0 if its <.25, 0.50 if it's between .25 and .75 and 1 if it's >.75. Seems like it's ignoring my if-statements, And on top of that the result I get isn't correct either. Some calculations ends up being negative, which I can't figure out why :/
double summa0 = vara - kontant; //item - change
var extrakt = (int)summa0; //removes decimals out of summa0 = 107
var avrundsSumma = summa0 - extrakt; //<--- extracts the decimals out of summa0
if (avrundsSumma < 0.25f)
{
avrundsSumma = Math.Floor(avrundsSumma);
}
else if (avrundsSumma > 0.75f) //Runs the decimals through if-statements
{
avrundsSumma = Math.Ceiling(avrundsSumma);
}
else
{
avrundsSumma = 0.5;
} // = in this case the result should be 1
double summa = extrakt + avrundsSumma; // 107 + 1 = 108
double attBetala = kontant - summa; // 500 - 108 = 392
Since I'm very new to this it's hard to know exactly which part of the code is causing the issue. When I run the code in CMD I get a negative result from "double summa = extrakt + avrundsSumma; // 107 + 1 = 108"
So instead of 108 I get -108.
Not sure what you mean by "Hard code the values" either :o
I have an event that comes through with a variable number of fields { "dev_1": 12, "dev_2": 34, ... }. What logstash filters should I look at to produce an average of those fields, something like { "device_avg": 23 }?
I don't know the exact field names ahead of time. I will need to do some pattern matching to average all fields that match dev_* but I'm not sure if that is something I can do simply with an aggregate filter
You would have to use a ruby filter. Try
ruby {
code => '
total = 0
count = 0
event.to_hash.each { |k, v|
if k =~ /^dev_/
count += 1
total += v.to_f
end
}
if count > 0 ; event.set("average", total/count) ; end
'
}
I'm trying to "compress" an array of 16 numbers ranging from 0 to 15 into a single number.
Since each element of the array is at most 15, I can represent it with just 4 bits, so I expect to have 4 bits * 16 = 64 bits which fits a number.
To do this "compression" I use the fallowing function:
function compressArray(array) {
return array.reduce(
(pre, curr, index) => {
return (pre | (curr * Math.pow(2, index * 4)));
},
0
);
}
But after index 8 it will keep giving me the same output without computing the correct |.
What am I missing here?
I am trying to use a nested for loop to create x and y coordinates for a method call. However, console.log shows that the loop variables are starting at the wrong value. Why is this? Here is my code:
for(let x = 0; x < 64; x++) {
console.log(x);
for(let y = 0; y < 32; y++) {
console.log(y);
}
}
This prints:
22
23
24
25
26
27
28
29
30
31
34
0
1
2
3
4
5
6
7
8
[values 9 - 30 omitted]
31
34
1
...and so on
Are you sure? I have tested this with node v8.9.1 and the code works for me as expected:
The outer loop starts at index 0 which gets printed on the console,
then the inner loop prints numbers 0 to 31.
In turn the outer loop continues at index 1 which gets printed on console and
then the inner loop prints 0 to 31,
and so on
May be you got confused with the output at some point. Thus, my suggestion is to prefix the console outputs with x and y as shown below.
for(let x = 0; x < 64; x++) {
console.log('x=' + x);
for(let y = 0; y < 32; y++) {
console.log('y=' + y);
}
}
You can also trial this on repl.it
I can tell you with relative confidence that the behaviour your print is describing is not the behaviour a for-loop of the kind you've written will yield.
In fact, copy-pasting your exact code to the Chrome console (which runs V8, the same as node) will yield the correct result:
Your issue lies either elsewhere in your code or in the tool you're looking at the logging in.
What are some uses of the modulus operator? I know that it calculates the remainder in division so really I am asking what uses does the remainder have?
So far I have used it to check if a number was even and alternate colors on a table.
for(int i=0;i<10;i++)
{
if((i % 2) == 0 )
{
// I'm in an even row
}else{
// I'm in an odd row
}
}
The most basic use
Note: lang used Java
Getting an indication of progress in a long running loop by printing a message once every so many iterations.
List<Thing> bigList = readBigList();
for (int i = 0; i < bigList.size(); i++) {
processThing(bigList.get(i));
if (i % 10000 == 0) {
LOG.info("Processed " + i + " out of " + bigList.size() + " items");
}
}
Unit conversion, e.g. 13425 m is 13425 / 1000 km and 13425 % 1000 m = 13 km and 425 m
random number trimming, if you're using C/C++'s rand(), a common idiom is rand() % (HIGH - LOW) + LOW to generate a random number between HIGH and LOW
modular arithmetic: angles are limited to 360 degrees or 2*pi, you can normalize their range using modulus operator
even/odd check: if "n % 2" is true then n is even otherwise it's odd
Primes
Convert numbers from base x to base y
72 minutes modulo 60 = 12 minutes past the hour
Bitwise calculations, including conditional checking.
Chinese arithmetic (is that the preferred nomenclature, dude?)
The modulus operator is the single-most important operator in Clock Arithmetic.
It's generally used to check if one number is evenly divisible by another.
if(number % 2 == 0){
// the number is even
} else {
// the number is odd
}
or
if(number % 3 == 0){
// the number is evenly divisible by three
} else {
// the number is not evenly divisible by three
}
If the result of a mod operation is 0, the dividend (number) is evenly divisible by the divisor.
You can take advantage of this to do things like "piano-keys" style alternate-row shading on table data, or printing new column headings every X number of rows, or what have you.
A programming 101 exapmle would be to modulate row colors for data:
for(int i = 0; i < 100; i++)
{
write-color i % 2;
}
Determine if a number is evan or odd:
return number % 2;