CodeIgniter Forums
Number Helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: Number Helper (/thread-3499.html)



Number Helper - El Forum - 10-06-2007

[eluser]Michael Wales[/eluser]
This definitely isn't anything to call home about, but I needed the functionality and didn't see it elsewhere in the framework - maybe it will help someone else out.

The Number Helper currently only has one function, append(), which accepts an integer parameter. This function will then determine what the correct appendage should be to turn than number in 1st, 2nd, 3rd, etc.

I haven't added in any error checking yet, but the core functionality is there.

/system/helpers/number_helper.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

function append($num) {
    switch (substr($num, -1)) {
        case 1:
            return $num . 'st';
        case 2:
            return $num . 'nd';
        case 3:
            return $num . 'rd';
        default:
            return $num . 'th';
    }
}

?>

Usage:
Code:
<?= append(1); ?>



Number Helper - El Forum - 10-06-2007

[eluser]Derek Allard[/eluser]
Nice work. While I guess it doesn't matter in this instance, it might be better to put it into system/application/helpers though (the system will still find it) as it avoids update problems in the off chance the CI implements its own number_helper in the future.


Number Helper - El Forum - 10-06-2007

[eluser]Michael Wales[/eluser]
I thought about that but I didn't see a precreated folder there (like libraries has an empty folder within the downloadable package). Might want to include that in 1.5.5, so there's a visual indicator that this is an acceptable place for your own helpers.


Number Helper - El Forum - 10-06-2007

[eluser]barbazul[/eluser]
You can have almost copy the folder structure from system into application (libraries, helpers, plugins...) since the algorithm which performs the file loading is the same for all....

Regarding the helper, I think there should be some numeric helper in the core. Derek is there any chance we'll be seeing this in a future release?

@walesmd: nice work done! (as you usually do)


Number Helper - El Forum - 10-06-2007

[eluser]llbbl[/eluser]
echo $fourth_post = append(4);


Number Helper - El Forum - 10-07-2007

[eluser]Derek Allard[/eluser]
Good point Micheal. I'll see about including a blank folder, and maybe also making a note in the userguide.

@barbazul - sure, there's a chance. But probably not for what we have here above. While I don't deny its useful, it is a single function, and probably isn't the type of thing that should be included in a framework by default. If it got fleshed out a bit, then absolutely - but I can't off hand think of any other useful number functions. Can you?


Number Helper - El Forum - 10-07-2007

[eluser]barbazul[/eluser]
without doing much thinking, the firsts that come to my mind (all of which I had to implement myself at some point):

* number to string conversions (like 4 to 'four', 19 to 'nineteen', etc),
* number 'types' conversions (like decimal, hex, roman, etc)
* money formatting (i don't know what else to call this)
* color parsing (i have worked on image manipulation many times and I had to do this on almost every case. I get a string like #FFFF00 and have to extract it into array('r'=>255,'g'=>255,'b'=>0) and that kind of thing)
* number padding
* large numbers comparison (PHP sucks when you have to work with really big numbers)

this are really small simple algorithms that I had to reproduce every time I needed them (not always in code igniter) and all of them could fit into a "number" helper.

Anyways, this is not really THAT necessary.... just following walesmd line of thought


Number Helper - El Forum - 10-07-2007

[eluser]Derek Allard[/eluser]
No, I agree. I'm not trying to say it isn't useful, we're just brainstorming here Wink


Number Helper - El Forum - 10-07-2007

[eluser]nmweb[/eluser]
The folks over at Cakephp came up with these:
Code:
The Number helper includes a few nice functions for formatting numerical data in your views.
precision
    * precision
    * mixed $number
    * int $precision = 3

Returns $number formatted to the level of precision specified by $precision.

toReadableSize
    * toReadableSize
    * int $sizeInBytes

Returns a human readable size, given the $size supplied in bytes. Basically, you pass a number of bytes in, and this function returns the appropriate human-readable value in KB, MB, GB, or TB.

toPercentage
    * toPercentage
    * mixed $number
    * int $precision = 2

Returns the given number formatted as a percentage, limited to the precision specified in $precision.

I don't see how I would often use these but some of the present helpers I also rarely use. Barbazul's suggestions are more useful Smile


Number Helper - El Forum - 10-07-2007

[eluser]llbbl[/eluser]
There are many math libraries that could be added.

http://www.phpmath.com/home

http://pear.php.net/pepr/pepr-proposal-show.php?id=312

Well as helpers or whatever. I still dont have all the different types of files straight. libraries, models, plugins, helpers ...