Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> zapisywanie do dwoch liczb po przecinku
dirtyhustlaz
post 27.05.2012, 21:20:23
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 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 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
kamil4u
post 27.05.2012, 23:13:09
Post #2





Grupa: Zarejestrowani
Postów: 2 350
Pomógł: 512
Dołączył: 4.01.2009
Skąd: Wrocław / Świdnica

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


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


--------------------
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 15.06.2025 - 04:28