function splitAmount(uint256 amount) private {
a1.transfer(amount.div(2));
a2.transfer(amount.div(2));
}
I've seen other threads on this but I feel like over complicate things. With this code the amount is evenly split between a1 and a2 with division by 2.
How would one do something like a 80/20 split with the same code?
80% is the same thing as
multiply by 80, and then divide by 100
as well as
multiply by 4, and then divide by 5
a1.transfer(amount.mul(4).div(5)); // 80% of `amount`
You can simplify the 20% in the same way:
multiply by 20 and then divide by 100
which is
multiply by 1, and then divide by 5
which is simply
divide by 5
a2.transfer(amount.div(5)); // 20% of `amount`
Related
I am working on a UK Profit and Loss Report in SSRS 2008R2 and am struggling with the percentage calculations
Here is an example to explain my question
Detail
Group1 Group2 Invoice Number Value %
Sales Total 810
Uk Sales 150
UK964423 50
UK452872 100
European Sales 450
dkkmalk 200
dkf682 250
Rest of World Sales 210
USA12353 100
CHIN25410 100
AFGAN14422 10
Variable Costs 455 56%
Material 200 25%
Sand 150
Steel 50
Wages 225 28%
Basic Pay 175
Overtime 50
Other Production Costs 30 4%
Packaging 20
Consumables 10
The percentage of 56% for the Variable costs is calculated as Variable Costs divided by Sales total (455/810). The Material percentage is similar Material total divided by Total Sales (200/810) and so on for Wages and Other Production Costs
How do I achieve these calculations please. In most cases I can get the nominator by such a formula in the group header as:
=sum(iif (GroupFieldName.Value="Variable Costs",FieldValue.Value,nothing)
But the denominator of Total Sales I can not seem to calculate!
Any suggestions welcome please, please bear in mind I would want to "future proof" the formulas / code solutions for SSRS2015 and SSRS 2017
Thanks in advance
Your expression isn't working correctly due to the grouping. The grouping separates your FieldValues so your SUM is only getting the total of the group.
The expression should use the SUM of your field for the group divided by all the values in your dataset.
=SUM(Fields!FieldValue.Value) / SUM(Fields!FieldValue.Value, "Dataset1")
Due to SSRS checking for divide by zero error, you might need to check for that possibility.
=IIF(SUM(Fields!FieldValue.Value, "Dataset1") = 0, 0, SUM(Fields!FieldValue.Value) )
/
IIF(SUM(Fields!FieldValue.Value, "Dataset1") = 0, 1, SUM(Fields!FieldValue.Value, "Dataset1")
With the IIFs, if the SUM is 0 the calculation is 0/1 which is 0 and avoids the Divide by Zero error.
ok, i'm not sure if i can explain this right.
Lets say i have a table with three columns (id, price, maxcombo)
maybe there's like 5 rows in this table with random numbers for price. 2. id is just incremental unique key)
maxcombo specified if that price can be in a combination of up to whatever number it is.
If x was 3, i would need to find the combination that has the maximum value of the sum 1-3 columns.
So say the table had:
1 - 100 - 1
2 - 50 - 3
3 - 10 - 3
4 - 15 - 3
5 - 20 - 2
the correct answer with be just row id 1.
since 100 alone (and can only be alone based on the maxcombo number)
is greater than say 50 + 20 + 15 or 20 + 15 or 10 + 20 etc.
Does that make sense?
I mean i could just calculate all the diff combinations and see which has the largest value, but i would imagine that would take a very long time if the table was larger than 5 rows.
Was wondering any math genius or super dev out there had some advice or creative way to figure this out in a more efficient manner.
Thanks ahead of time!
I built this solution to achieve the desired query. However, it hasn't been tested in terms of efficiency.
Following the example of colums 1-3:
SELECT max(a+b+c) FROM sample_table WHERE a < 3;
EDIT:
Looking at:
The correct answer will be just row id 1
...I considered maybe I misunderstood your question, and you want the query just obtain the rowid. So, I made this other one:
SELECT a FROM sum_combo WHERE a+b+c=(
SELECT max(a+b+c) FROM sum_combo WHERE a > 3
);
Which would for sure take too long in larger tables than just 5 rows.
I'm a bit of a noobie working with SSRS 2008 and I'm having trouble trying to get the average of a sum of numbers. Here is how my report looks like (I couldn't put in a screen shot, hopefully the text table I created makes sense ):
Example |
ParentCode | ChildCode | Total Grade | A | B | A% | B%
P1
1234 5 1 4 20% 80%
Totals 5 1 4 20% 80%
P2
4567 6 3 3 50% 50%
5555 3 2 1 66.7% 33.3%
Totals 9 5 4 58.35% 41.65%
I am calculating the percentage of grades for each ChildCode (ie. code 5555 has 2 A's out of 3 grades total, so its A% = 66.67%). I am doing this by using scope descriptors:
=sum(Fields!grade.Value)/sum(Fields!grade.Value,"gradePercCol")
Where the first sum's scope is by the row grouping (ChildCode) and the second sum is by the grade percent column grouping.
For whatever reason, I need to get the average of these percentages. Ie:
For parent code P2, I need the average for column A%: (50% + 66.67%) / 2 = 58.35, as well as for the B% column and so on. The "Totals row" in my example would be an example of what I need.
I think I need to make a custom aggregate since the original percentages are calculated using the sum function and I can't get an average of an aggregate function. However, I'm having a lot of trouble understnding the documentation for custom aggregates.
http://blogs.msdn.com/b/robertbruckner/archive/2008/07/20/using-group-variables-in-reporting-services-2008-for-custom-aggregation.aspx
http://technet.microsoft.com/en-us/library/bb934256(v=sql.100).aspx
Any help clarifying these links or pointing me in the right direction would be much appreciated. Thanks a lot!
Do your clients realize that they are averaging averages?
I think the best solution would be to derive your grade percentage in your data set. If you can do that, then you can just do a avg(percentage) in your group footer.
How would you calculate the total number of possibilities that binary can have in one byte?
00000000 through 11111111 = num_of_possibilities
The total number is 2 to the power of the number of bits. So, eight bits has 28 possible values.
If you really mean "how to compute it", consider that each bit has two possible values.
So one bit implies 2 values.
Two bits has one set of two values of each possible value of the other bit, so
00
01
10
11
which means a total of 4 (= 2×2) values.
Three bits gives four values twice, or 8 (=4×2) values. Four bits, 8×2; five bits, 16×2, and so on.
So eight bits is 2×2×2×2×2×2×2×2 or 256.
It is a simple question: The number of possibilities is 2n where n is the number of bits.
So for 1 byte, which is 8 bits, there are 28 possibilites, 256.
There are several methods:
2^n where n is the number of bits (2^8) Each bit has 2 possibilities.
Unsigned value of all 1's + 1 (255 + 1) Count up from 0 to max value (all ones) + zero.
Build a tree where each leaf is the sum of the values to right and left of the new value from the row above. Possibilities is sum of row having n+1 entries. (2 ( 1 + 8 + 28 + 56 ) + 70) Each value is probability of that number of bits from 0 to n.
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.