The Letter E Posted January 3, 2011 Share Posted January 3, 2011 Hey Everybody, I was replying to someone elses question about number formatting and came up with a class that will format a number of any length with decimal or not, without rounding. It works perfectly, but after a comment by a noteworthy member my curiosity was aroused. Is there an easier/faster/better way to achieve this?? My Object: <?php /*GHETTO NUMBER FORMATTER v1.0*/ class ghettoNumber { private $input; private $output; public $whole; public $decimal; public $number; function __construct($input){ $this->input = $input; $this->output = $output; $this->number = $number; $this->whole = $whole; $this->decimal = $decimal; } private function ghettoFormat(){ //Split number at the .(decimal) $this->number = explode('.', $this->input); //Define the whole number $this->whole = $this->number[0]; //Format the whole number $this->whole = number_format($this->whole, 0,'', ','); //Define the decimal $this->decimal = $this->number[1]; //Format the decimal $this->decimal = rtrim($this->decimal, '0'); if(is_string($this->input)){ $this->output = $this->decimal != '' ? $this->whole.'.'.$this->decimal : $this->whole; } else{ $this->output = '<strong>ERROR:</strong> Input arg must be passed as type: <em>string</em>'; } //Return result return 'Original - '.$this->input.'<br> GhettoFormat - '.$this->output.' <br><br>'; } public function makeItGhetto(){ return $this->ghettoFormat(); } } ?> Sample Instantiation: $ghettoFormat = new ghettoNumber('12565456565.123401201001210000'); print $ghettoFormat->makeItGhetto(); I will reiterate my question once more... Is there, in your opinion, a BETTER way to achieve this (number formatting w/o rounding)? Thanks People, E Quote Link to comment Share on other sites More sharing options...
MMDE Posted January 3, 2011 Share Posted January 3, 2011 what can I use this for that I can't do with the number_format function? http://php.net/manual/en/function.number-format.php Quote Link to comment Share on other sites More sharing options...
The Letter E Posted January 3, 2011 Author Share Posted January 3, 2011 what can I use this for that I can't do with the number_format function? http://php.net/manual/en/function.number-format.php That's where the meat of my question lies, I have not been able to achieve a 'non-rounded' number using number_format(), is there an arg to that I am not using?? Thus far I have not been able to achieve this format without the number being rounded. Quote Link to comment Share on other sites More sharing options...
MMDE Posted January 3, 2011 Share Posted January 3, 2011 what can I use this for that I can't do with the number_format function? http://php.net/manual/en/function.number-format.php That's where the meat of my question lies, I have not been able to achieve a 'non-rounded' number using number_format(), is there an arg to that I am not using?? Thus far I have not been able to achieve this format without the number being rounded. A number doesn't need to have a decimal, but if they do, they can either have non-infinite and infinite number of decimals... When it's infinite number of decimals you must in the end round to get closest to the actual number, not rounding would be wrong... anyways, if the number has freaking many digits: http://www.phpfreaks.com/forums/php-coding-help/make-a-number-readable/msg1499622/#msg1499622 anyways, give us an example of what you want to do Quote Link to comment Share on other sites More sharing options...
The Letter E Posted January 3, 2011 Author Share Posted January 3, 2011 what can I use this for that I can't do with the number_format function? http://php.net/manual/en/function.number-format.php That's where the meat of my question lies, I have not been able to achieve a 'non-rounded' number using number_format(), is there an arg to that I am not using?? Thus far I have not been able to achieve this format without the number being rounded. A number doesn't need to have a decimal, but if they do, they can either have non-infinite and infinite number of decimals... When it's infinite number of decimals you must in the end round to get closest to the actual number, not rounding would be wrong... anyways, if the number has freaking many digits: http://www.phpfreaks.com/forums/php-coding-help/make-a-number-readable/msg1499622/#msg1499622 anyways, give us an example of what you want to do The example is exactly what my object already does, just wondering if there is a more efficient way to achieve the same effect. Example: Number: 11554516516.215015651156510151001561000 Formatted: 11,554,516,516.215015651156510151001561 Number: 1234.5056000 Formatted: 1,234.5056 Number: 12.50 Formatted: 12.5 Number: 12345.000 Formatted: 12,345 As you can see, it keeps the number EXACTLY the same as the input. It just adds commas for thousands delimiters and removes 0's from the end of the decimal, does not round or change the value of the number in any way. As far as I can tell you cannot achieve this same effect using only number_format(), am I wrong? Quote Link to comment Share on other sites More sharing options...
MMDE Posted January 3, 2011 Share Posted January 3, 2011 if I understand what you mean correctly, then you could just use preg_replace... or am I wrong then? brb Quote Link to comment Share on other sites More sharing options...
The Letter E Posted January 3, 2011 Author Share Posted January 3, 2011 if I understand what you mean correctly, then you could just use preg_replace... or am I wrong then? brb I don't feel like this thread is summed up as easily as right and wrong. Yes, you're right, preg_replace would be one possible function that could be used to do a work around for the same effect. I'm simply looking for the MOST EFFICIENT way to do this. If there is a way to do it using only number_format() I would definitely defer to that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.