Jump to content
php.lv forumi
  • 0

Kā pareizi skaitīt naudu?


Question

Posted

Sveiki

 

Man ir radusies neliela problēmiņa saskaitot naudu.

 

<input type="radio" onclick="cad0(this.value);" value="Ls">
<input type="radio" onclick="cad0(this.value);" value="Eur">

<script type="text/javascript">
function cad0(val) {
 switch(val) {
  case "Ls": money="0.75"; break;
  case "Eur": money="0.55"; break;
 }
 // ir 5 vertibas pa 1 uz augšu
 price2 = money * 2;
 price3 = money * 3;
 price4 = money * 4;
 price5 = money * 5;
 saveinhtml('jdx-15-23-92',price);
 saveinhtml('jdx-15-23-93',price2);
 saveinhtml('jdx-15-23-94',price3);
 saveinhtml('jdx-15-23-95',price4);
 saveinhtml('jdx-15-23-96',price5);
}
</script>

 

problēma ir sekojoša, ka javascript sareikina teiksim 0.521521 vai kautko tādu, bet jalietoju Math.Round tad attiecīgi paliek tikai 1 skaitlis.

ka pareizi reikinat lai attiecigi javascriptaa

price = 0,75

price2 buutu 1,50

price3 buutu 2,25

price4 buutu 3,00

price5 buutu 3,75

 

? paldies jau ieprieksh

4 answers to this question

Recommended Posts

  • 0
Posted

Round(x*100)/100.

Vēl tik pielikt pārbaudi, lai izvada trailing '0' simbolus.

 

Kāpēc tu tajā switch'ā money piešķir stringu? Tālāk tu taču ar money mainīgo operē kā ar skaitlisku vērtību!

  • 0
Posted (edited)

uj tiesham pareizi, paldies bubu par tiem stringiem.

varbuut vari sikak nedaudz paradit to kodu ? pameiginaju man nesanaca.

 

v2=Round((v*2)*100)/100;

sanak ka itka izvada (1.2) butu jabut (1.25)

Edited by EdgarsA
  • 0
Posted

atradu kodu kurs man palidzeja. :) paldies

function fam(mnt){
   mnt -= 0;
   mnt = (Math.round(mnt*100))/100;
   return (mnt == Math.floor(mnt)) ? mnt + '.00' 
             : ( (mnt*10 == Math.floor(mnt*10)) ? 
                      mnt + '0' : mnt);
}

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...