Rounding amount in javascript - numbers

I'm trying to round the amount in my form's total, tried several methods provided in different threads but none worked for me.
my form url is http://indushospital.org.pk/qurbani/
The amount shown in total is multiplied with 2.5% additional charges due to which its showing the amount like USD 186.63372, I want to show it like 186.64 Or simply 187.
The additional charges formula is mentioned below:
function getAmountPlusCharges(amount) {
// additionalCharges would result here '250' when '20000' is passed as amount.
var additionalCharges = (amount * 2.564) / 100;
return additionalCharges + amount;
Please help, I
'm not familiar with java functions at all
Thanks in advance.

You just have to use Math.round, then you can use to fixed to make sure its always 2 decimal places.
function getAmountPlusCharges(amount) {
return (Math.round(amount * 102.564 ) / 100).toFixed(2);
}

Related

Math Equation in Ticket App Not Formatting Correctly

I’m having a difficult time formatting my math equation to calculate and format correctly. When I try to add the child ticket to the total price function, it formats the total price label very oddly. It formats the tickets totals separately instead of together. It has something to do with my function for calculating price.
My total price block of code is as follows:
totalPriceLabel.text = String(adultTicket.price * Double(adultTicketsSelected)) + String(childTicket.price * Double(childTicketsSelected))
Any help is appreciated.
to keep it simple while you get the hang of things, calculate the value first:
let price = adultTicket.price * Double(adultTicketsSelected) + childTicket.price * Double(childTicketsSelected)
You could use string interpolation to then convert this to your text:
totalPriceLabel.text = "\(price)"
but you'd be better using either a number formatter or String(format:)
totalPriceLabel.text = String(format: "%.2f", price)
I'll let you research the NumberFormatter yourself. :-)

Rounding to two decimal places is not working

I am trying to reduce the decimal places of my number to two. Unfortunately is not possible. For this reason I added some of my code, maybe you will see the mistake...
Update [dbo].[company$Line] SET
Amount = ROUND((SELECT RAND(1) * Amount),2),
...
SELECT * FROM [dbo].[company$Line]
Amount in db which I want to change:
0.00000000000000000000
1914.65000000000010000000
376.81999999999999000000
289.23000000000002000000
Result I get after executing the code:
0.00000000000000000000
1366.28000000000000000000
268.89999999999998000000
206.38999999999999000000
Result I want to get (or something like this):
0.00000000000000000000 or 0.00
1366.30000000000000000000 or 1366.30
268.99000000000000000000 or 268.99
206.49000000000000000000 or 206.49
RAND() returns float.
According to data type precedence the result of multiplying decimal and float is float, try:
ROUND(CAST(RAND(1) as decimal(28,12)) * Amount, 2)
this should do the trick.

How to in calculate percentages in swift?

I am creating an expense ios application. I am using a Progress View to display how much income the user has left. So for example, if the user puts $100 income, and $30 expense, I'd like the bar to be 70% full. How do I calculate this?
I had originally put
let fractionalProgress = Float(expenseFloat!)/Float(balanceFloat!)
but this doesn't return the right value needed.
The formula in your question is correct if you want to know the percentage spent so far.
The percent remaining formula is "remaining balance" / "original balance".
And of course "remaining balance" is "original balance" - "total of expenses".
let fractionalProgress = (balanceFloat - expenseFloat) / balanceFloat
where balanceFloat is the original balance and expenseFloat is the total expenses.
In your example with a balanceFloat of 100 and an expenseFloat of 30 this gives (100 - 30) / 100 which is 0.7.
Of course you would multiple this result by 100 if you wish to show a percentage. Better yet, use a NumberFormatter setup with a .percent style.
We can calculate percentage in swift as below:
//Calucate percentage based on given values
public func calculatePercentage(value:Double,percentageVal:Double)->Double{
let val = value * percentageVal
return val / 100.0
}
We can use this function like:
let value = calculatePercentage(value: 500,percentageVal: 20)
print(value)
Output will be:
100

Getting the total number of records in PagedList

The datagrid that I use on the client is based on SQL row number; it also requires a total number of pages for its paging. I also use the PagedList on the server.
SQL Profiler shows that the PagedList makes 2 db calls - the first to get the total number of records and the second to get the current page. The thing is that I can't find a way to extract that total number of records from the PagedList. Therefore, currently I have to make an extra call to get that total which creates 3 calls in total for each request, 2 of which are absolutely identical. I understand that I probably won't be able to rid of the call to get the totals but I hate to call it twice. Here is an extract from my code, I'd really appreciate any help in this:
var t = from c in myDb.MyTypes.Filter<MyType>(filterXml) select c;
response.Total = t.Count(); // my first call to get the total
double d = uiRowNumber / uiRecordsPerPage;
int page = (int)Math.Ceiling(d) + 1;
var q = from c in myDb.MyTypes.Filter<MyType>(filterXml).OrderBy(someOrderString)
select new ReturnType
{
Something = c.Something
};
response.Items = q.ToPagedList(page, uiRecordsPerPage);
PagedList has a .TotalItemCount property which reflects the total number of records in the set (not the number in a particular page). Thus response.Items.TotalItemCount should do the trick.

Regarding BigDecimal

I have a csv file where amount and quantity fields are present in each detail record except header and trailer record. Trailer record has a total charge values which is the total sum of quantity multiplied by amount field in detail records . I need to check whether the trailer total charge value is equal to my calculated value of amount and quantity fields. I am using the double data type for all these calculations. When i browsed i am able to understand from the below web link that it might create an issue using double datatype while comparison with decimal points. It's suggesting to using BigDecimal
http://epramono.blogspot.com/2005/01/double-vs-bigdecimal.html
Will i get issues if i use double data type. How can i do the calculations using BigDecimal. Also i am not sure how many digits i will get after decimal points in csv file. Also amount can have a positive or negative value.
In csv file
H,ABC.....
"D",....,"1","12.23"
"D",.....,"3","-13.334"
"D",......,"2","12"
T,csd,123,12.345
------------------------------ While Validation i am having the below code --------------------
double detChargeCount =0;
//From csv file i am reading trailer records charge value
String totChargeValue = items[3].replaceAll("\"","").trim();
if (null != totChargeValue && !totChargeValue.equals("")) {
detChargeCount = new Double(totChargeValue).doubleValue();
if(detChargeCount==calChargeCount)
validflag=true;
-----------------------While reading CSV File i am having the below code
if (null != chargeQuan && !chargeQuan.equals("")) {
tmpChargeQuan=Long(chargeQuan).longValue();
}
if (null != chargeAmount && !chargeAmount.equals("")) {
tmpChargeAmt=new Double(chargeAmount).doubleValue();
calChargeCount=calChargeCount+(tmpChargeQuan*tmpChargeAmt);
}
I had declared the variables tmpChargeQuan, tmpChargeAmt, calChargeCount as double
Especially for anything with financial data, but in general for everything dealing with human readable numbers, BigDecimal is what you want to use instead of double, just as that source says.
The documentation on BigDecimal is pretty straight-forward, and should provide everything you need.
It has a int, double, and string constructors, so you can simply have:
BigDecimal detChargeCount = new BigDecimal(0);
...
detChargeCount = new BigDecimal(totChargeValue);
The operators are implemented as functions, so you'd have to do things like
tmpChargeQuan.multiply(tmpChargeAmt)
instead of simply tmpChargeQun * tmpChargeAmt, but that shouldn't be a big deal.
but they're all defined with all the overloads you could need as well.
It is very possible that you will have issues with doubles, by which I mean the precomputed value and the newly computed value may differ by .000001 or less.
If you don't know how the value you are comparing to was computed, I think the best solution is to define "equal" as having a difference of less than epsilon, where epsilon is a very small number such as .0001.
I.e. rather than using the test A == B, use abs(A - B) < .0001.