Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Remove Unnecessary Decimals With Php?
Amezis
post Sep 7 2006, 05:43 PM
Post #1


Privileged Member
*********

Group: Members
Posts: 535
Joined: 14-February 05
From: Oslo, Norway
Member No.: 3,759



I have a MySQL DECIMAL field allowing up to 3 decimals to the number. However, many of the numbers only have one decimal. An example is 5.2. I want the script to echo ONLY 5.2 and not 5.200. However, if the number is 5.211, I want it to echo the whole number, so I can't just use the round() function. Is there a way to do this?
Go to the top of the page
 
+Quote Post
shadowx
post Sep 7 2006, 08:15 PM
Post #2


A clever man learns from his own mistakes, a WISE man learns from those of OTHERS
***********

Group: [HOSTED]
Posts: 1,028
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719



**EDIT**

sorry completely missed this part
QUOTE
so I can't just use the round() function.


However i tested round() on numbers like "5.11" and as ive posted below by rounding to three decimals it will do nothing to change the number unless it ends in a zero in which case it simply strips the zero and nothing more. which explains why this posts assumes you dont understand round();! sorry! But ill leave it as it is for other people to read and hopefully it will be a good solution to your problem smile.gif

**/ edit***

there is a built in function called round(); which i just found in the php manual, link: http://uk2.php.net/round

Basically it rounds any decimal number to a spcified number of digits after the decimal point, eg:

QUOTE

round(*NUMBER*, *DECIMAL PLACES*);

round(3.001, 3);

this would output "3.001" because it ends in a one and is being rounded to three decimal places therefore nothing is changed as it already is 3 DP's

round(3.010, 3);

This would give "3.01" as you can see the last zero has been stripped.



there is a limit to this function though because it cant handle numbers containg commas such as "1,000.32" so providing your numbers arent formatted with commas for every thousand and million etc...you will be fine to use this function. The link i gave also has a lot of info in the case your numbers are formatted with commas.


This post has been edited by shadowx: Sep 7 2006, 08:18 PM
Go to the top of the page
 
+Quote Post
Amezis
post Sep 7 2006, 08:34 PM
Post #3


Privileged Member
*********

Group: Members
Posts: 535
Joined: 14-February 05
From: Oslo, Norway
Member No.: 3,759



Oh, I didn't know that it would strip the extra zeros. Well, thanks, it works now smile.gif
Go to the top of the page
 
+Quote Post
jlhaslip
post Sep 8 2006, 03:25 AM
Post #4


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,072
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



There is also a method to format output from the print command.
Check out the php.net manual for the printf function or the sprintf function.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics


 



- Lo-Fi Version Time is now: 11th October 2008 - 08:46 PM