Remove Unnecessary Decimals With Php?

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > PHP Programming

Remove Unnecessary Decimals With Php?

Amezis
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?

Reply

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

 

 

 


Reply

Amezis
Oh, I didn't know that it would strip the extra zeros. Well, thanks, it works now smile.gif

Reply

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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. mysql remove decimal - 4.61 hr back. (1)
  2. php mysql decimal point - 6.86 hr back. (1)
  3. number of decimals php - 53.23 hr back. (1)
  4. how to strip decimal number in php - 53.83 hr back. (1)
  5. php format decimal zeros - 67.79 hr back. (1)
  6. php remove decimal places - 78.47 hr back. (1)
  7. remove decimal points php - 87.81 hr back. (1)
  8. php remove decimals - 34.79 hr back. (3)
  9. php delete zero after decimal - 91.78 hr back. (1)
  10. round decimals php - 97.12 hr back. (1)
  11. php remove decimal from time - 103.26 hr back. (1)
  12. php remove decimal function - 107.01 hr back. (1)
  13. php remove decimal point - 70.10 hr back. (2)
  14. how to check if a number has a decimal php - 112.07 hr back. (1)
Similar Topics

Keywords : remove, unnecessary, decimals, php

  1. Remove A Value From A Php Array Based On Its Value
    (5)
  2. Remove Objects From Array With Unset
    (2)
    Have been trying to solve this for a long time now, so I thought Iīd give you guys a chance to break
    it! I want to remove an object from an array that I store in a Session object. Iīve written a
    function that takes an array and an id as parameters. Checks if one of the objects has the id I want
    to remove and then itīs supposed to remove it. Quite simple, thatīs why I cant see why it doesnt
    work! Hereīs the code: CODE function Remove_prod($id2,$AL) {
    print_r($AL); foreach ($AL as $prod) if($prod-....

    1. Looking for remove, unnecessary, decimals, php

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for remove, unnecessary, decimals, php

*MORE FROM TRAP17.COM*
advertisement



Remove Unnecessary Decimals With Php?



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE