How to fix decimal places of a Uint128 variable in rust - rust

Rust newbie here. I am trying to calculate amount deducted from a value based on rate and decimal places in rate.
Without the decimal places, suppose if I want to deduct 2% amount from original (where 2 is the rate), this is how I am accomplishing it:
let original = token_value[0].amount;
token_value[0].amount = original * Decimal::percent(98);
What if I want to also define the decimal places in rate and then make the deduction, how will I accomplish that? The crates that I used are cosmwasm_std::{Decimal,Uint128}

If you want to take 2% of original amount as fees
you can do
// Here we hardcode the swap fees as 2/100 or 2% of from_amount
fn get_swap_fee(from_amount: Uint128) -> StdResult<Uint128> {
from_amount
.checked_mul(Uint128::from(2u128))
.map_err(StdError::overflow)?
.checked_div(Uint128::from(100u128))
.map_err(StdError::divide_by_zero)
}

Related

Error while Multiplying a u64 to u128 in Rust?

I want to multiply a u128 number with a u64 number. u64 will always be less than or equal to one. Amount is always greater than 1000000(1 million)
fn testing(amount:u128, starting_time:u64, ending_time:u64, duration:u64){
return amount*((ending_time - starting_time:u64)/duration)
}
Even if the number should return something like 500,000 it always returns zero unless the ((ending_time - starting_time:u64)/duration) equals 1 then the return value always amounts.
I want to give less weightage to someone if they enter the last to the lottery. Both starting and ending times are u64 because they are in UNIX timestamps.
Example: Let's say Alice entered the lottery at time 0. Same time the lottery was started. So Alice would get full weightage for the amount she deposited let's say she deposited 30000 so she will get 30000 tickets.
Bob entered the lottery halfway through the lottery means that whatever he deposits he will get half the weightage of the deposited amount. If he deposits 30000 he will get 15000 tickets.
The best solution I found is:
(amount/1000000) * (( ((end_time - start_time)*1000000 )/duration ) as u128)
I see 2 problems with your code:
You are trying to subtract 2 timestamps, but is that going to leave you with a timestamp? or a number? and if it is the number, is that in seconds or milliseconds or what? and does duration also represent seconds or milliseconds or is it another timestamp?
If all that code still makes sense, then you have a second problem:
Your expression: ending_time - starting_time)/duration, presumably you are expecting it to be a fraction between 0 & 1, right? well if all your variables are ints, then your result is going to be an int, which means guess what? no fraction.
You need to cast all that stuff to floats, or multiply the amount first so you don't have to worry about the fraction. Maybe try this:
return amount*(ending_time - starting_time)/duration;

How do I calculate the number of years to target in Microsoft Excel

If I have an average YOY growth of say 2.5%, current year sales of $500,000, and target sales of $1,000,000. Is there a way to calculate the number of years until I hit my target (assuming a continued YOY growth rate of 2.5%), without calculating each additional years's sales on a different row?
You can use the NPER function for this:
=NPER(Growth,0,currentSales,-targetSales)
Note that this gives the same result as #Dominique, but is using a built-in Excel function.
Also, by virtue of the nature of Excel financial functions, the signs for the current and future Sales need to be different.
There's a simple formula for this:
Money_end = (1+r/100)^Y*Money_begin
Where:
Money_begin = the starting amount of money, in this case 500000.
Money_end = the finishing amount of money, in this case 1000000.
r = the percent ratio, in this case 2.5.
Y = the amount of years, which you are looking for.
So, your question comes down to solving this equation:
1000000 = (1+2.5/100)^Y * 500000
2 = 1.025^Y
Y = log(2)/log(1.025)
If you want this to be solved by Excel, you might use the formula and use the Solver basic Excel feature for coming up with the same (but numerical instead of analytical) result.

How to calculate DEF total using RRDTOOL

I am trying to calculate the total octets_in using python rrdtool. I have this but it's not giving me the correct output.
here is a data sample
[
406.29947919463086
],
[
433.0391666666667
],
[
430.70380365448506
]
I want the total to be 1269
my def, cdef and vdef are
f'DEF:OCTETS_IN={self.file_name}:OCTETS_IN:AVERAGE'
'CDEF:octets_in=OCTETS_IN,PREV,ADDNAN',
'VDEF:out_octets_in_total=octets_in,AVERAGE'
The only operators I can use from rrdtool are AVERAGE, MINIMUM, MAXIMUM and PERCENT and they all give the wrong results.
Anybody know how to do this?
If you want to calculate the total of a rate variable over the time period, then there is a useful VDEF function to calculate sum( var x stepsize )
For example,
DEF:octetspersec=file.rrd:IN:AVERAGE
VDEF:octetstotal=octetspersec,TOTAL
Now, octetstotal holds the total number of octets over the graph time window based on the rate held in the octetspersec variable.
If you have an older version of RRDTool, you may not have the TOTAL function. In this case, use AVERAGE, then multiply by STEPWIDTH and the pixel width of the graph.
Note that if your variable already holds the number of bytes for that interval, then you will not need to multiply by the step width. Since the TOTAL function does this (as it assumes the variable is a rate) you then need to divide the VDEF result by STEPWIDTH again.
For more details on using the RPN functions, see here

Truncate to the nearest thousandths and ignore the remainder of the value

Can MS Excel do rounding but only up to the nearest thousandths place and ignore the rest of the decimals in a formula? I've tried value, fixed, round and none of these do what I need.
Let's say I have 100 square feet of space and I pay $1.00566399 per sq foot, I need the formula to round only up to the 5 and ignore the rest of the numbers because when you speak on longer terms it makes a difference in rate.
So I should be multiplying 100sf by $1.01 = $101.00 and not get 100sf * 1.00566399 = $101.57
=trunc(A1,5)
If you want to round up, maybe something like
=trunc((A1*10000+1)/10000,5)
Use the TRUNC($cellRef or number, Decimal places) function. It reduces the number of decimal places WITHOUT rounding, then do your math.
So for example:
=TRUNC(1.00566399,3)
=A1*ROUNDUP(B1,2)
Where A1 contains the number of square feet and B1 contains the price per square foot in it's original long decimal form.

excel formula for working handling two different tax brackets

I'm making a spreadsheet in excel to help me understand how starting a part-time business on top of my full time job might affect my tax. I need to do this because I'm very close to my next UK tax bracket and I want to know exactly how it might affect my finances.
As part of this I'm trying to write an excel formula that will add two numbers then check if this new number is greater than a third number. I will refer to the two numbers added together as A and the third number as B. If A is not greater than B then I will multiply by 20% to find out how much of it is owed in tax. If A is greater than B I will subtract B from A to create number C and multiply B by 20% and C by 40% and add the two together to produce the final number I need. Can someone please xplain to me how to script this in Excel? I've looked for online examples but I'm not finding the language very penetrable. If I was using a scripting language I was more familiar with the code would look like this:
float taxThreshold = 42011.00;
int income = foo;
if(taxThreshold < income)
{
float higherRate = income-taxThreshold;
float standardTaxOwed = taxThreshold * 20%;
float higherRateOwed = higherRate * 40%;
float finalTaxOwed = standardTaxOwed+higherTaxOwed;
}
else
{
float finalTaxOwed = income * 20%;
}
I'm sure this is very simple to do, I just don't get the excel syntax. Can someone show me how this should be done? You'd not only be solving this problem but also giving me a means of translating from my current scripting knowledge into excel.
The easiest way is to store your 4 input parameters in some cells - and then name them. To do so, select the cell and type a meaningful name into the field that shows the address (i.e. left of the formula bar).
Once you have done this, you can simply use this formula:
=IF(Income>Threshold,Threshold*StandardTaxRate+(Income-Threshold)*HighTaxRate,Income*StandardTaxRate)
Without the naming, the formula would be something like this:
=IF(B2>B1,B1*B7+(B2-B1)*B8,B2*B7)
Both formulas will calculate you the tax you'd need to pay. If you want it more explicit, I'd recommend a layout such as this:
The formulas would read:
C7: =IF(B2>B1,B1,B2)
C8: =IF(B2>B1,B2-B1,0)
D7: =B7*C7
D8: =B8*C8
HTH!

Resources