Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> zapisywanie do dwoch liczb po przecinku
dirtyhustlaz
post
Post #1





Grupa: Zarejestrowani
Postów: 22
Pomógł: 0
Dołączył: 29.03.2012

Ostrzeżenie: (0%)
-----


Mam pproblem, chce zeby moj skrypt zaipisywal tylko do dwoch liczb po pprrzecinku wiem ze powienieniem uzyc toFixed ale po moich probach nie dziala nic (IMG:style_emoticons/default/wink.gif) takze mam problem z wyswietlaniem total jak uzylem parseFloat() zamiast parseInt() moglby ktos na to spojrzec i powiedziec mi gdzie mam uzyc toFixed i zeby poprawnie wyswietlalo mi wynik total

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Untitled Document</title>
  6. <script>
  7.  
  8. var nameOfBook = Array(), priceOfBook = Array(), index = 1, total = 0;
  9.  
  10. function addBook()
  11. {
  12. nameOfBook[index] = document.getElementById('name').value;
  13. //we changing type of variable from string to int
  14. priceOfBook[index] = parseFloat(document.getElementById('price').value);
  15.  
  16. document.getElementById('name').value = '';
  17. document.getElementById('price').value = '';
  18. index++;
  19.  
  20. }
  21. //we get data fromm form and put in array
  22. function displayBooks()
  23. {
  24. for(var i = 1; i < index; i++)
  25. {
  26.  
  27. document.write(i+". "+nameOfBook[i]+" - "+priceOfBook[i]+"<br>");
  28. total = total+priceOfBook[i];
  29. }
  30.  
  31. document.write("<br><b>Total: </b>"+total);
  32. }
  33. // print out list of books with price we added before and total price
  34. </script>
  35. </head>
  36. <body>
  37. <div id="books">
  38. <label>Enter name of book: </label><input id="name" type="text">
  39. <br><br><label>Enter price of book: </label><input id="price" type="text">
  40. <br><button onClick="addBook()">Add</button>
  41. </div>
  42. <br><br>
  43. <button onClick="displayBooks()">Calculate</button>
  44.  
  45.  
  46. </body>
  47. </html>


udalo mi sie juz zrobic zeby wyswietlalo dwa miejsca po przecinku ale nie dziala mi dalej podliczanie total, moze mi ktos pomoc (IMG:style_emoticons/default/wink.gif) ? ?!

  1. ok, udalo mi sie juz zapisysywac liczby w ulamkach dziesietnych i wyswietla tylko do dwoch miejsc po przeciknku ale dalej nie dziala mi podliczanie total
  2.  
  3. <!--c1--><div class='codetop'>Kod</div><div class='codemain'><!--ec1--><!DOCTYPE HTML>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>Untitled Document</title>
  8. <script>
  9.  
  10. var nameOfBook = Array(), priceOfBook = Array(), index = 1, total = 0;
  11.  
  12. function addBook()
  13. {
  14.     nameOfBook[index] = document.getElementById('name').value;
  15.     //we changing type of variable from string to int
  16.     priceOfBook[index] = parseFloat(document.getElementById('price').value);
  17.     document.getElementById('name').value = '';
  18.     document.getElementById('price').value = '';
  19.     index++;
  20.     
  21. }
  22. //we get data fromm form and put in array
  23. function displayBooks()
  24. {
  25.     for(var i = 1; i < index; i++)
  26.     {
  27.  
  28.         document.write(i+". "+nameOfBook[i]+" - "+priceOfBook[i].toFixed(2)+"<br>");
  29.         total = total+priceOfBook[i];
  30.             }
  31.     
  32.     document.write("<br><b>Total: </b>"+total);
  33. }
  34. // print out list of books with price we added before and total price
  35. </script>
  36. </head>
  37. <body>
  38. <div id="books">
  39.     <label>Enter name of book: </label><input id="name" type="text">
  40.     <br><br><label>Enter price of book: </label><input id="price" type="text">
  41.     <br><button onClick="addBook()">Add</button>
  42. </div>
  43. <br><br>
  44. <button onClick="displayBooks()">Calculate</button>
  45.  
  46.  
  47. </body>
  48. </html><!--c2--></div><!--ec2-->
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.12.2025 - 14:02