Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Jak zaokrągli wynik do 2 miejsc po przecinku
szczabik
post 5.06.2008, 21:41:01
Post #1





Grupa: Zarejestrowani
Postów: 226
Pomógł: 1
Dołączył: 13.05.2008

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


Jak zaokrągli wynik do 2 miejsc po przecinku

Zrobiłem tak i coś nie chce działać
  1. <?php
  2. $srednia = dbquery("SELECT AVG(rating_vote) FROM ".$db_prefix."ratings WHERE rating_type='P' AND rating_user='".$userdata['user_id']."'");[/size]
  3.  
  4. [size="3"]echo "".number_format(dbresult($srednia, 2, '.', ' '))."";
  5. ?>


A jak zrobie tak
  1. <?php
  2. $srednia = dbquery("SELECT AVG(rating_vote) FROM ".$db_prefix."ratings WHERE rating_type='P' AND rating_user='".$userdata['user_id']."'");[/size]
  3.  
  4. [size="3"]echo "".number_format(dbresult($srednia, 0))."";
  5. ?>



To działa ale zaokrągla mi wynik do pełnej wartości czyli np 5 a chce by były 2 miejsca po przecinku wie ktoś jak to zrobić

Go to the top of the page
+Quote Post
help_mee
post 5.06.2008, 21:43:20
Post #2





Grupa: Zarejestrowani
Postów: 244
Pomógł: 21
Dołączył: 10.02.2008
Skąd: Załęcze Wielkie

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


http://pl2.php.net/manual/pl/function.number-format.php
----> http://pl2.php.net/manual/pl/function.round.php
dla mnie troszkę źle zbudowane...

Ten post edytował help_mee 5.06.2008, 21:45:56


--------------------
Amentis dolet!
Deus et patria!
» Pomogłem? Kliknij Pomógł.
Go to the top of the page
+Quote Post
pyro
post 5.06.2008, 21:43:58
Post #3





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

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


  1. <?php
  2. $srednia = dbquery("SELECT AVG(rating_vote) FROM ".$db_prefix."ratings WHERE rating_type='P' AND rating_user='".$userdata['user_id']."'");
  3. echo "".round(dbresult($srednia), 2)."";
  4. ?>


i nie dawaj bbcode w znacznikach PHP ^^

Ten post edytował pyro 5.06.2008, 21:50:30


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
webdice
post 5.06.2008, 21:43:59
Post #4


Developer


Grupa: Moderatorzy
Postów: 3 045
Pomógł: 290
Dołączył: 20.01.2007




Manual nie gryzie. Poczytaj o mysql round.
Go to the top of the page
+Quote Post
ppp
post 5.06.2008, 21:44:21
Post #5





Grupa: Zarejestrowani
Postów: 33
Pomógł: 3
Dołączył: 29.01.2008

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


http://pl.php.net/round


--------------------
if the specifications are absolutely ambiguous, every program will satisfy them
Go to the top of the page
+Quote Post
sarat20
post 6.06.2008, 09:23:59
Post #6





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 17.02.2008

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


Używająć poniższej funkcji pad_zeros() możesz wymusić by liczba zawsze była formatowana z 2 miejscami po przecinku:
  1. <?php
  2. $val = 4.1;
  3. $val = round($val, 2);
  4. $val = pad_zeros($val, 2);
  5. echo $val;
  6.  
  7. function pad_zeros($number, $zeros)
  8. {
  9. if (strpos($number, '.') !== false) {
  10. preg_match('#.(d+)$#', $number, $match);
  11. $number .= str_repeat('0', $zeros-strlen($match[1]));
  12. return $number;
  13. } else {
  14. return $number.'.'.str_repeat('0', $zeros);
  15. }
  16. }
  17. ?>
Go to the top of the page
+Quote Post
erix
post 6.06.2008, 13:38:22
Post #7





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




ROTFL haha.gif
15 linijek zamiast jednej. haha.gif

number_format" title="Zobacz w manualu PHP" target="_manual...


--------------------

ZCE :: Pisząc PW załączaj LINK DO TEMATU i TYLKO w sprawach moderacji :: jakiś błąd - a TREŚĆ BŁĘDU? :: nie ponaglaj z odpowiedzią via PW!
Go to the top of the page
+Quote Post
sarat20
post 6.06.2008, 14:45:42
Post #8





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 17.02.2008

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


Oops, tyle lat w pehapie, a nie znałem tej funkcji...
Każdemu może się zdarzyć winksmiley.jpg
/out
Go to the top of the page
+Quote Post
szczabik
post 6.06.2008, 14:55:01
Post #9





Grupa: Zarejestrowani
Postów: 226
Pomógł: 1
Dołączył: 13.05.2008

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


Zrobiłem wszytko tak jak podaliście i nie działa zrobiłem tak
  1. <?php
  2. echo "".round(dbresult($srednia, 2))."";
  3. ?>

To zwraca mi 0 co jest nie prawdą zrobiłem tak
  1. <?php
  2. echo "".dbresult($srednia, 0)."";
  3. ?>


To zaokrągla mi do 4 miejsc po przecinku

Już nie wiem co jest nie tak
Go to the top of the page
+Quote Post
Petermechanic
post 6.06.2008, 14:58:32
Post #10





Grupa: Zablokowani
Postów: 47
Pomógł: 7
Dołączył: 6.05.2007

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


  1. <?php
  2. echo "".round(dbresult($srednia), 2)."";
  3. ?>
Go to the top of the page
+Quote Post
szczabik
post 6.06.2008, 15:04:08
Post #11





Grupa: Zarejestrowani
Postów: 226
Pomógł: 1
Dołączył: 13.05.2008

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


Zrobiłem tak i dalej nie działa

Ten post edytował szczabik 6.06.2008, 15:04:20
Go to the top of the page
+Quote Post
webdice
post 6.06.2008, 15:08:24
Post #12


Developer


Grupa: Moderatorzy
Postów: 3 045
Pomógł: 290
Dołączył: 20.01.2007




Tylko po co robić coś w PHP skoro można to zrobić w MySQL?

  1. SELECT ROUND(`rate`, 2) AS `rate` FROM `table`
Go to the top of the page
+Quote Post
szczabik
post 6.06.2008, 15:11:27
Post #13





Grupa: Zarejestrowani
Postów: 226
Pomógł: 1
Dołączył: 13.05.2008

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


Mam takie zapytanie i jak mam to tu wstawić
  1. <?php
  2. SELECT AVG(rating_vote) FROM ratings
  3. ?>
Go to the top of the page
+Quote Post
webdice
post 6.06.2008, 15:27:16
Post #14


Developer


Grupa: Moderatorzy
Postów: 3 045
Pomógł: 290
Dołączył: 20.01.2007




Obiecuje Ci, że jeśli troszkę pomyślisz to naprawdę nic Ci się nie stanie.

  1. SELECT ROUND(AVG(`rating_vote`), 2) AS `rating_vote` FROM `ratings`
Go to the top of the page
+Quote Post
szczabik
post 6.06.2008, 15:33:49
Post #15





Grupa: Zarejestrowani
Postów: 226
Pomógł: 1
Dołączył: 13.05.2008

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


Teraz działa dziekuje bardzo
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: 26.04.2024 - 17:59