PHP subtract a percentage result - numbers

$temp_price_1 = round($price * ((100-$sale_percentage) / 100), 2); $price = $temp_price_1;
im using this code to subtract a percentage from a number, for this example my starting number for $price is 24.99 and the $sale_percentage is 50.0 this result is the $temp_price_1 being 12.5 which is correct, however i need it to be 12.50 so it looks like a correct currency value. How do i add the 0 back on the end.
Many thanks

Check out the number_format function (returns a string):
number_format($price, 2);
Will return the string "12.50" when $price is set to 12.5

Related

flutter - return/show plus sign of int

I'm trying to build some training app for functions for school. The problem is:
Everytime the randomly picked number is lower than 0, my function shows +-, because
I have a fixed format for my function.
EXAMPLE
I tried to use the NumberFormat of the Intl-Package, but then I can't use the int-values correctly. Is there a way to show a plus sign for positive numbers, while they are still usable to work with them?
Code so far:
int randomNumberMinMax(int min, int max){
int randomminmax = min + Random().nextInt(max - min);
if(randomminmax==0){
randomminmax = min + Random().nextInt(max - min);
}
//generate random number within minimum and maximum value
return randomminmax;
}
int a = randomNumberMinMax(-5, 5);
int b = randomNumberMinMax(-10, 10);
int c = randomNumberMinMax(-10, 10);
String task = "f(x) = $a(x+$b)²+ $c";
You could only show the plus when the number is positive like this for example
String task = "f(x) = $a(x${b >= 0 ? "+" : ""}$b)²${c >= 0 ? "+" : ""} $c";

Swift unreliable round() method to 2 decimal places

I'm trying to calculate the time between two doubles (distance, speed) to 2 decimal places using swift round() method but there are instances where its unreliable and I end up with something like 58.000000001. I tried to hack fix this by rounding again but even that doesn't work on larger numbers eg 734.00000001 Is there a way to fix this or another way to calculate time between two doubles?
var totalTime: Double = 0
for 0...100 {
let calcTime = 35.3 / 70
let roundedTime = round(calcTime * 100) / 100.0
print("TIME === \(roundedTime)")
totalTime += round(totalTime * 100) / 100 + roundedTime // round again to clamp the extra zero's
print("Total time \(totalTime)")
}

How to calculate the percentage(%) in postgresql

I am not able to calculate the % in postgresql
Below the my code
v_total_repro_count = (select count(pm_task_run_bug_repro_id) from project.pm_task_run_bug_repro
where pm_task_run_bug_detail_id = 605)--5;
v_repro_count = (select count(pm_task_run_bug_repro_id) from project.pm_task_run_bug_repro
where execution_status = 'Reprod' and pm_task_run_bug_detail_id = 605)--3;
v_impact = select (5/3)*100; = answer = 100
v_impact = select (3/5)*100; = answer = 0
the answer getting 0 and 100 instead of 60%
Cast value to numeric
SELECT ROUND(( 3::NUMERIC/5::NUMERIC ) * 100);
If you want floating point results, you should use at least one float in your calculation, such that the entire expression is promoted to floating point:
select (3.0 / 5) * 100; -- returns 60

t-sql nearest quarter hour

I'm looking for a way to return a value adjusted to the 'highest' quarter. I cannot round because the value returned cannot be greater than the original value. For example if the value is 53.290 I need to return 53.25 and 51.49 would return 51.25. Can I do this in t-sql?
The answer you've provided, Steve, of dividing by 0.25 has the same effect as this code. I'm not sure, but I feel that multiplication by four is clearer than divison by a fraction.
SELECT (FLOOR(#myValue * 4)) / 4.0
After working on this for a bit and doing some more research I think this is the best solution. Thanks everyone for the responses.
DECLARE #myvalue DECIMAL(8,4);
SET #myvalue = 53.26;
SELECT (Floor(#myvalue/.25)) * .25; -- Returns 53.25
SET #myvalue = 53.24;
SELECT (Floor(#myvalue/.25)) * .25; -- Returns 53.00
DECLARE #Value DECIMAL(10,4) = 53.290;
SELECT ROUND(#Value/25.00, 2) * 25
Result: 53.250000000
Rounded value to TWO Digits
SELECT CONVERT(DECIMAL(18, 2), ROUND(#Value/25.00, 2) * 25)
Result: 53.25
This will get the nearest quarter based on the values present on the right side of the decimal.I have just tried my hands on it. Hope it would be useful. Just try it with different values as well.
DECLARE #Value DECIMAL(10, 2) = 53.49
SELECT CASE
WHEN CAST(RIGHT(#Value, 2) AS FLOAT) > 0
AND CAST(RIGHT(#Value, 2) AS FLOAT) <= 49
THEN FLOOR(#Value) + 0.25
WHEN CAST(RIGHT(#Value, 2) AS FLOAT) > 49
AND CAST(RIGHT(#Value, 2) AS FLOAT) <= 74
THEN FLOOR(#Value) + 0.50
ELSE CEILING(#Value) - 0.25
END

crystal reports conditional formatting for summary fields

I'm trying to create a decimal formatting formula on my summary fields. The values in the database could have 0, 1, or 2 decimal places. I've started with this:
If (CurrentFieldValue mod 1 = 0) Then
0
Else If (CurrentFieldValue mod .1 = 0) Then
1
Else
2
On a simple single data field, this works and displays the value with 0, 1, or 2 decimal places based on the data coming from my database. The same formula doesn't work for a summary field on my reports with group data. Any ideas?
Edit: Since I don't know how to format code in a comment, I'll address the suggestion of using a formula here:
Didn't work. Formula:
Sum ({myTable.dataValue}, {myTable.groupField})
then I used:
If ({#formula} mod 1 = 0) Then
0
Else If ({#formula} mod .1 = 0) Then
1
Else
2
And I still got whole numbers for everything. My rounding is set to .01 with no formula. Do I need a formula for rounding too? I still don't understand why this works on individual values but not for group summaries.
OK- it turns out this is due to our lack of understanding of the mod function :)
Everything mod 1 actually returns 0. This is the formula you need to use:
if {ER100_ACCT_ORDER.ER100_ORD_TOT} * 100 mod 100 = 0 then
0
else if {ER100_ACCT_ORDER.ER100_ORD_TOT} * 100 mod 10 = 0 then
1
else
2
:)
How about just creating a formula field instead of using the built-in summary field:
sum({mytable.myfield})
Then you can use your conditional formatting:
If ({#formula} mod 1 = 0) Then
0
Else If ({#formula} mod .1 = 0) Then
1
Else
2