Angular 2 Rounding numbers - percentage - angularjs

How can i generate this percentage: number * 100 ( ex. 0.050 * 100 = 5.5 )
i get the number from db like 0.050 but need to be showed as 5.5
actual code
<strong>{{d.AllowancePercentage}} %</strong>

The simplest way I found is this:
{{ (d.AllowancePercentage * 100) | number:'1.1-2' }}
Here's the official documentation: https://angular.io/api/common/PercentPipe

Related

Issue while adding values in SQL Server

Please read again till end (description updated)
I want something like this.
ex :
if (7200 / 42) is float then
floor(7200/42) + [7200 - {(floor(7200/42)) * 42}] / 10 ^ length of [7200 - {(floor(7200/42)) * 42}]
STEP : 1 => 171 + ((7200 - (171*42))/10 ^ len(7200-7182))
STEP : 2 => 171 + ((7200 - 7182)/10 ^ len(18))
STEP : 3 => 171 + (18/10 ^ 2)
STEP : 4 => 171 + (18/100)
STEP : 5 => 171 + 0.18
STEP : 6 => 171.18
I have written the code in SQL which actually works perfectly but the addition of 171 + 0.18 only gives 171
IF I can get "171/18" instead of "171.18" as string then it'd also be great. (/ is just used as separator and not a divison sign)
Following is the code I written
Here,
(FAP.FQTY + FAP.QTY) = 7200,
PRD.CRT = 42
(values only for example)
select
case when PRD.CRT <> 0 then
case when (FAP.FQTY + FAP.QTY)/PRD.CRT <> FLOOR((FAP.FQTY + FAP.QTY)/PRD.CRT) then --DETERMINE WHETHER VALUE IS FLOAT OR NOT
(floor((FAP.FQTY + FAP.QTY)/PRD.CRT)) +
((FAP.FQTY + FAP.QTY) - floor((FAP.FQTY + FAP.QTY)/PRD.CRT) * PRD.CRT) /
POWER(10, len(floor((FAP.FQTY + FAP.QTY) - floor((FAP.FQTY + FAP.QTY)/PRD.CRT) * PRD.CRT)))
else
(FAP.FQTY + FAP.QTY)/PRD.CRT -- INTEGER
end
else
0
end
from FAP inner join PRD on FAP.Comp_Year = PRD.Comp_Year and
FAP.Comp_No = PRD.Comp_No and FAP.Prd_Code = PRD.Prd_Code
I got all the values correct till 171 + 0.1800 correct but after that I am only receiving 171 in the addition. I want exactly 171.18.
REASON FOR THIS CONFUSING CALCULATION
Its all about accounting
Suppose, a box(or a cartoon) has 42 nos. of items.
A person sends 7200 items. how many boxes he has to send?
So that will be (7200/42) = 171.4257.
But boxes cannot be cut (its whole number i.e 171).
so 171 * 42 ie 7182 items.
Remaining items = 7200 - 7182 = 18.
So answer is 171 boxes and 18 items.
In short 171.18 or "171/18"
Please help me with this..
Thank you in advance.
Recognise that you're not producing an actual numeric result, I'd describe it as unhealthy to try to keep it using such a datatype1.
This produces the strings you're seeking, if I've understood your requirement:
;With StartingPoint as (
select 7200 as Dividend, 42 as Divisor
)
select
CONVERT(varchar(10),Quotient) +
CASE WHEN Remainder > 0 THEN '.' + CONVERT(varchar(10),Remainder)
ELSE '' END as FinalString
from
StartingPoint
cross apply
(select Dividend/Divisor as Quotient, Dividend % Divisor as Remainder) t
(Not tested for negative values. Some adjustments may be required. Technically % computes the modulus rather than the remainder, etc)
1Because someone might try and add two of these values together and I doubt that produces a correct result, not even necessarily if using the same Divisor to compute both.
Just another idea about how to calculate it.
Simple calculate the whole boxes.
And concatinate a dot with the remaining items (using a modulus).
Wrapped it all up in a CASE WHEN (or IIF) to avoid the divide by zero.
Example snippet:
declare #TestTable table (FQTY numeric(18,2), QTY numeric(18,2), CRT numeric(18,0));
insert into #TestTable (FQTY,QTY,CRT) values
(5000, 2200, 42),
(5000, 2200, 0),
( 100, 200, 10);
select *,
(CASE
WHEN CRT>0
THEN CONCAT(CAST(FLOOR((FQTY+QTY)/CRT) as INT),'/',CAST((FQTY+QTY)%CRT as INT))
ELSE '0'
END) AS Boxes
from #TestTable;
Result:
FQTY QTY CRT Boxes
------- ------- --- ------
5000.00 2200.00 42 171/18
5000.00 2200.00 0 0
100.00 200.00 10 30/0
The CONCAT returns a varchar, and so does the CASE WHEN.
But you could wrap that CASE WHEN in a CAST.
You're getting an automatic type conversion from int to decimal(10,0) which is probably not what you want.
https://learn.microsoft.com/en-us/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-2017
Check out the "Caution" box.
If you want a specific amount of precision, you'll need to explicitly cast() the values to the desired data type.
if i understand your logic correctly you want the remainder of 7200 divide by 42 and the remainder is to divide by 100
declare
#dividend int = 7200,
#divisor int = 42
select (#dividend / #divisor)
+ convert(decimal(10,4),
(#dividend % #divisor) * 1.0 / power(10, len(#dividend % #divisor)))
EDIT: change to handle the 10^len(remainder)

Difference in precision between AVG function and sum-divide

Similar questions have been asked before, but none have addressed why the AVG function in MSSQL produces a value different from explicit SUMand-divide when no NULL values are present in the data.
I would like to calculate the score for the data in this (simplified) TestTable.
CorrectCount IncorrectCount
5.0 0.0
3.0 2.0
5.0 0.0
4.0 0.0
3.0 0.0
5.0 0.0
2.0 1.0
5.0 0.0
5.0 0.0
2.0 2.0
The score is calculated as score = sum(CorrectCount) / sum(CorrectCount+IncorrectCount)
The following query
SELECT
AVG(CorrectCount / (CorrectCount+IncorrectCount)) as ScoreAverage,
SUM(CorrectCount) / SUM(CorrectCount+IncorrectCount) as ScoreSumDivide
FROM TestTable
produces this output:
ScoreAverage ScoreSumDivide
0.876666666666667 0.886363636363636
Where does this difference come from? What does AVG do different than the SUMand-divide? I am looking for an explanation why the result of the AVG is different from the explicit SUM(CorrectCount) / SUM(CorrectCount+IncorrectCount). I expect it is due to precision or rounding internal to the AVG function.
Probably my comment was not understood, so I am expanding it here. Assume you have Correct/Incorrect counts as:
5/2
3/1
Averaging Correct/(Correct+Incorrect) means (5/7 + 3/4)/2 = 41/56
However Sum( Correct) /sum(Correct+Incorrect) means ( 5+3 )/(7+4) = 8/11
41/56 != 8/11
If you did : Sum(Correct /(Correct+Incorrect))/Count it would be: (5/7 + 3/4)/2 = 41/56 which is equal to avg.
It is simply how the math works out. Your score average takes an average of the individual percentages. If C1, C2 ā€¦ Cn is your correct scores and I1, I2 ā€¦ In is your incorrect scores and ā€œNā€ is the number of records then the math will look as follow:
C1/(C1+ I1) + C2 /(C2 + I2) + ... + Cn /(Cn + In)
-------------------------------------------------
N
Your sum average first sum all of your correct scores and sum all your total scores and then calculate the percentage ratio. The math of this look as follow:
C1 + C2 + ... + Cn
----------------------------------------------
(C1+ I1) + (C2 + I2) + ... + (Cn + In)
Both numbers are meaningful but the second will better reflect what the percentage of correct counts where for the entire data set.
I think you are simply calculating different stuff there. The aquivalent for the AVG should be SUM(CorrectCount / (CorrectCount + IncorrectCount)) / COUNT(*).
SELECT
AVG(CorrectCount / (CorrectCount+IncorrectCount)) as ScoreAverage,
SUM(CorrectCount / (CorrectCount + IncorrectCount)) / COUNT(*) ScoreSumDivide
Your average calculation wants modification;
,AVG(CorrectCount) / (AVG(CorrectCount)+AVG(IncorrectCount)) as ScoreAverage
This returns the correct value of 0.886363 (39 / 44) rather than what looks like a rounding issue without it.

Angular filter maximum decimals

I want to create a new filter such that i get an input, and i give the amount of maximum decimals to show and return a string that is formatted according to locale.
Input(number) Output for 1 decimal Output for 2 decimals
1.01 1 1.01 / 1,01
1.001 1 1
1.1 1.1 / 1,1 1.1 / 1,1
1 1 1
1000 1 000 / 1.000 / 1,000 1 000 / 1.000 / 1,000
I want to use angular's built in number filter for locale purposes, but i can't come up with how to remove decimals since if i do it after number filter, then i have a locale specific string, and i cant do it before, since i don't know how to round before i have used number:x
Any hints or ideas?
Added my own filter, implemented as such:
app.filter('numberNoDecimalsIfZeroFilter', function($filter) {
return function(value, fractionSize) {
//If has no decimals, then don't show
if(value%1 === 0){
fractionSize = 0;
}
return $filter('number')(value,fractionSize);
}
});
Used as such:
<div class="form-group">
<label class="control-label">Amount: </label>
{{::value.amount | numberNoDecimalsIfZeroFilter}}
</div>
You can't, see filter's implementation:
// format fraction part.
while (fraction.length < fractionSize) {
fraction += '0';
}
The filter always adds zeros. If you want to achieve what you want, you have to write your own filter.

In c , how do I make 1200 / 500 = 3

In C , how do I make 1200 / 500 = 3.
I'm doing a homework assignment.
Shipping Calculator: Speedy Shipping company will ship your package based on how much it weighs and how far you are sending the package. They will only ship small packages up to 10 pounds. You need to have a program that will help you determine how much they will charge. The charges are based on each 500 miles shipped. They are not pro-rated, i.e., 600 miles is the same charge as 900 miles.
Here is the table they gave you:
Package Weight--------------------------Rate per 500 miles shipped
2 pounds or less------------------------$1.50
More than 2 but not more than 6---------$3.70
More than 6 but not more than 10--------$5.25
Here is one test case.
Test Case Data:
Weight: 5.6 pounds
Miles: 1200 miles
Expected results:
Your shipping charge is $11.10
My answer keeps coming out to 7.40
Are you trying to round up? Before dividing, you could add 499 to the number that is being divided.
(0 + 499) / 500 -> 0
(1 + 499) / 500 -> 1
(1200 + 499) / 500 -> 3
This will round up.
Say you want to get a ceiling division a by b (in your example a = 1200 b = 500).
You can do it in integer arithmetic like this.
result = (a + b - 1) / b;
Or you could use floating point numbers and do it like this (probably a bad idea)
result = (int) ceil( (double) a / b );
The thing is that as this is a homework, you could just make it up in small steps:
if( a % b == 0 ) {
result = a / b;
} else {
result = a / b + 1;
}
Another advantage of this code is that it actually doesn't overflow for too big as, but this is not relevant in this case, I guess.
I'd suggest using the mod and truncate functions. If mod comes out zero, it's fine, otherwise truncate and add 1.
You have to use the ceiling of the division. This will round the quotient up to the next integer.
So when you are trying to find the number of 500-mile increments, you have to round the quotient up to the next integer.
Alternatively, (and inefficiently), you could increment the number of miles by 1, until it is divisible by 500...that is, while ( (q = x_miles++%500) != 0 ) {} . Then multipy q by the rate to get your answer (That is also assuming you will have an integer number of miles).
You could also use the stdlib div function. This might be nice if you only wanted integer math and specifically wanted to avoid floating point math.
http://www.cplusplus.com/reference/clibrary/cstdlib/div/
#include <stdlib.h>
int foo(void)
{
div_t result = div(1200, 500);
return result.quot + (0 < result.rem);
}
[EDIT1]
From your code you would implement this part as follows:
if ( weight <= 5.6 )
{
int multiplier = (int) miles / 500;
if( ((int)miles % 500) > 0)
multiplier++;
rate370 = (double)multiplier * 3.7;
printf("Your total cost : %.2lf\n", rate370);
}
[ORIGINAL]
In "integer land" 1200 / 3 should equal to 2.
for what it "seems" you want try this:
int multFiveHundreds = (int)totalWeight / 500;
if(multFiveHundreds % 500 > 0)
multFiveHundreds++;

Ruby on rails 3 - Show decimals

Probably one of the simpler questions but I haven't found a solution for it. I want to show 2 decimals after making an equation of numbers coming from a database.
This is the code I have.
(((#best.price * #amount) + #best.retailer.profile.shippingCost)/(#best.productSize.productSize * #amount))
number_with_precision is your friend:
number = (((#best.price * #amount) + #best.retailer.profile.shippingCost)/(#best.productSize.productSize * #amount))
Then in your view:
number_with_precision(number, :precision => 2)

Resources