anonīms Posted April 21, 2011 Report Posted April 21, 2011 Ka panākt, ka tiek saskaitīti divi var, kuri ir ar komatiem? tb basket_money = Math.round(basket_money+delivery); alert(basket_money); basket_money = 0.71 delivery = 3.90 izvada 6, nez kapēc. Ar veseliem skaitļiem viss ok Quote
0 codez Posted April 21, 2011 Report Posted April 21, 2011 nez, man izvada 5: http://jsfiddle.net/hYxxL/ Quote
0 anonīms Posted April 21, 2011 Author Report Posted April 21, 2011 Tas nemaina faktu, ka ir jābūt 4.61 :/ Quote
0 anonīms Posted April 21, 2011 Author Report Posted April 21, 2011 (basket_money + delivery).toFixed is not a function[break On This Error] basket_money = (basket_money+delivery).toFixed(2); basket_money = (basket_money+delivery).toFixed(2); //alert(basket_money); $('#cart_item_price_total').html('Ls '+basket_money); ;/ Quote
0 briedis Posted April 21, 2011 Report Posted April 21, 2011 (edited) basket_money = basket_money + delivery; //alert(basket_money); $('#cart_item_price_total').html('Ls ' + basket_money.toFixed(2)); Edited April 21, 2011 by briedis Quote
0 rATRIJS Posted April 21, 2011 Report Posted April 21, 2011 Math round noapaljo ciparus, taadeelj tu neredzi ciparus aiz komata. Es dariitu taa: basket_money = Math.round((basket_money + delivery) * 100) / 100; Quote
0 anonīms Posted April 21, 2011 Author Report Posted April 21, 2011 briedi, tavam tas pats errors. var basket_money = $('#cart_item_price_total').html(); basket_money = basket_money.substr(3); basket_money = Math.round((basket_money + delivery) * 100) / 100; $('#cart_item_price_total').html('Ls ' + basket_money); Izvada "Ls NaN" substr tiek lietotots, jo tiek aizvākts "Ls " pirms tiek aprēķināts Quote
0 anonīms Posted April 21, 2011 Author Report Posted April 21, 2011 vot kur noslēpums. basket_money = parseFloat(basket_money); Quote
0 indoom Posted April 21, 2011 Report Posted April 21, 2011 (edited) gļukains tas float 0.71 + 3.90 = 4.609999999999999 (0.71 + 3.90) * 100 / 100 = 4.609999999999999 0.07 + 0.08 = 0.15000000000000002 function addNumbers() { for (var c=0,i=0,l=arguments.length;i<l;++i) { if (!isNaN(arguments[i])) { c += Math.round(arguments[i] * 100); } } return c / 100; } addNumbers(0.71, 3.90); //4.61 addNumbers(0.07,0.08); // 0.15 Edited April 21, 2011 by indoom Quote
0 Toms Posted April 21, 2011 Report Posted April 21, 2011 (edited) indoom, kāpēc jāsarežģī? basket_money = Math.round((basket_money+delivery)*100)/100; alert(basket_money); Edit: var original=28.453 1) //round "original" to two decimals var result=Math.round(original*100)/100 //returns 28.45 2) // round "original" to 1 decimal var result=Math.round(original*10)/10 //returns 28.5 3) //round 8.111111 to 3 decimals var result=Math.round(8.111111*1000)/1000 //returns 8.111 Edited April 21, 2011 by Toms Quote
0 codez Posted April 21, 2011 Report Posted April 21, 2011 Tas nemaina faktu, ka ir jābūt 4.61 :/ Kādu faktu, ka round noapaļo skaitļus līdz veseliem? LOL! Quote
0 v3rb0 Posted April 21, 2011 Report Posted April 21, 2011 tā ka neviens vēl pateicis - naudas jau vispār nebūtu jāglabā kā float un kur nu vēl kaut kur klienta browserī kaut ko apaļot. Quote
0 Toms Posted April 21, 2011 Report Posted April 21, 2011 Var gan apaļot, jo tā nereti ir ērtāk (ja ir forma ar vairākām pozīcijām, kur klients komplektē gala summu). Protams, tas viss pēc submita tiek pārrēķināts ar PHP drošības nolūkos. Quote
0 codez Posted April 21, 2011 Report Posted April 21, 2011 Piekrītu verbo, ka naudas daudzums jāglabā int. Parasti glabājot mazāko vienību - santīmus, centus. Tas nepieciešams, lai apaļojot nesāktos kopējā naudas daudzuma gadījumrakstura mainīšanās. Quote
0 Toms Posted April 21, 2011 Report Posted April 21, 2011 oj, pārpratu apaļošanu. Neņemt mani vērā :) Quote
Question
anonīms
Ka panākt, ka tiek saskaitīti divi var, kuri ir ar komatiem?
tb
basket_money = 0.71
delivery = 3.90
izvada 6, nez kapēc. Ar veseliem skaitļiem viss ok
16 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.