Jump to content
php.lv forumi
  • 0

math.round gļuko


Question

Posted

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

Recommended Posts

  • 0
Posted
(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);

 

;/

  • 0
Posted (edited)
basket_money = basket_money + delivery; 
//alert(basket_money);
$('#cart_item_price_total').html('Ls ' + basket_money.toFixed(2));

Edited by briedis
  • 0
Posted

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

  • 0
Posted (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 by indoom
  • 0
Posted (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 by Toms
  • 0
Posted

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.

  • 0
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...