I am looking for a way to round the decimal portion of numbers up or down to the nearest .25, .5, .75, or whole number - ms-access

If the decimal part is 0.1 to 0.12, it rounds down to the next lower integer
If the decimal part is 0.13 to 0.37 it rounds to 0.25
If the decimal part is 0.38 to 0.62 it rounds to 0.5
If the decimal part is 0.63 to 0.87 it rounds to 0.75
If the decimal part is 0.88 or more, it rounds up to the next higher integer

Multiply by 4, round to the nearest integer, divide by 4?

There is a general method for this:
Multiply your number by 4.
Round to the nearest integer.
Divide by 4.
In SQL:
ROUND(column * 4) / 4

I don't know the exact function name, but basically you use floor(4*x)/4. floor might be called int, to_int, or something like that.

Related

Frantions to binary

Learning how to convert numbers from integers to binary.
I'm working on a fraction of .36 the binary for it is .01011... I understand that to get the binary if a fraction you times the number by 2 and read from the top number down.
So
.36 = 0 First number
.36 x 2 = .72 =1 , it's still below zero
.72 x 2 = 1.44 = 0, as it as it's above zero
1.44 x2 = 2.88 = 1, this is were I get thrown, is it becouse the .88 is closer to 1?
2.88 x2 = 5.76 =1
Giving me the .01011
So is it everything above .5 =1? so
I'm starting to play with floating point numbers so really need to know how to convert binary fractions
Your method is correct.
Some intuition: to convert an integer to base 2, you repeatedly take mod 2, giving your next digit, then divide by 2. Fractions are similar: think of it as converting to base 1/2: repeatedly take mod 1/2 (1 if the fractional part has 1/2, 0 otherwise), then divide by 1/2.

MySQL structure for storing numeric values that differ drastically

I have a column with numeric values that differ drastically:
123.072
2
0.00012
851
1234
0.1
12
0.531211
etc.
I've tried using float and decimal but when I enter 0.023 for example, it just rounds the value as 0. I can't afford to round these numbers.
What structure type and length should I be using?
Try defining the range(length) of your decimal:
decimal(number,number)

MySQL Round Up to Nearest 5 Cents

I need to round up values using MySQL to increments of 5 cents (0.05). It has to ALWAYS round up. Examples:
0.01 -> 0.05
2.12 -> 2.15
0.16 -> 0.20
How can I accomplish this. I tried a few things with ceil() and round(), but it seems like I can use some help from a MySQL expert.
Since 100/5 = 20 all we do is multiply by 20, round up to the nearest whole number, and then divide by 20.
(To round up in MySQL use CEIL)
Example for $1.03:
1.03 * 20 = 20.60
CEIL(20.60) = 21.0000
21 / 20 = 1.0500
NOTE: I also round the whole statement to 2 decimals places just to remove any trailing zeros.
SELECT ROUND( CEIL( (old_price) * 20) / 20, 2) FROM table
EDIT: A generalized example (notice the 100.00 to cast the value as non-integer)
SELECT ROUND( CEIL ( (old_price) * (100.00/the_increment)) / (100.00/the_increment), 2) FROM table
SELECT floor((<value> + 0.025) * 20) / 20
Try to use this one -
SELECT (<value> DIV 0.05) * 0.05 + IF(<value> MOD 0.05 = 0, 0, 0.05)

Subtracting binary with numbers of different lengths

I am trying to solved: 1111 – 10010 (binary)
I would like to use two's compliment to solve it. I realize that the answer will be negative, but I don't know how to get it. I tried putting a zero before the first number (01111) to give the numbers equal number of 1s and 0s. Also, how will I know the answer is negative?
01101
+ 00001
____________
01110 <-- two's compliment
01110
+01111
________
11101 //this isn't right
I think the easiest way to solve this problem is to beak it into small steps.
My first assumption is that you are trying to solve 15 (1111 (binary)) - 18 (10010 (binary))
I find the easiest way to do subtraction in two's complement is by the method of complements which is instead of trying to subtract the positive 18 from the positive 15 ( +15 - (+18)), we instead add negative 18 to positive 15 ( +15 + (-18) ). This has the same result but is easier to do in two's compliment ( note: you can't do this if your number system does not have negative numbers)
So we have to take the number 15 and -18 and convert them into two's complement numbers. Since 18 is represented in binary with 5 bits we need to use at least 6 six bit to represent -18 in twos complement.
To convert -18 into two's complement we take 18 in two's complement 010010 flip those bits (turn the 0s into 1s and 1s into 0s) 101101, and then to add one to the flipped bits using binary addition
1 (carried digits)
101101 (-19 (flipped 18 ))
+ 000001 (1)
_________
101110 (-18)
To convert 15 into two's complement we take 15 in binary (1111) and then we add zeros on the left until it has the same amount of digits as -18 (101110) 001111
Now that we both numbers in two's complement we can add them together using binary addition
111 (carried digits)
001111 (15)
+ 101110 (-18)
_________
111101 (-3)
This give us -3 in two's complement which is the correct answer (15 - 18 = -3).
You can learn about two's complement looking at the twos complement wiki page

Converting hex to binary and one and two's complement in 16 bits

I am trying to convert FFAD (hex) to a decimal value and then do one and two's complement on it. FFAD is represented as a 16 bit integer.
When I convert FFAD to base 2 I get 1111111110101101.
My problem is how I know if it is a negative number or not?
I have the binary, now to do ones complement normally I would change the last bit from a 0 to a 1 and then flip all the bits, but as a 16 bit integer I don't have any more available bits. Since the 16th bit is a 1 does that mean it is a negative number? How would I do the complement of that? I am just all around confused with this problem and any pointers would be greatly appreciated.
Its negative because it is left padded with ones. Left padding with zeros would indicate a positive number. For example, here are the decimal values for three bit numbers:
011 = 3
010 = 2
001 = 1
000 = 0
111 = -1
110 = -2
101 = -3
100 = -4
Notice, numbers that are left padded with ones are negative and numbers that are left padded with zeros are positive.
Standard procedure for 2's compliment is to negate your bits, and then add 1.
1111111110101101 becomes 0000000001010010. Then you add 1 to get 0000000001010011.