Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CakeNumber::toPercentage should allow separator localization #1955

Closed
ghost opened this issue Oct 11, 2013 · 9 comments
Closed

CakeNumber::toPercentage should allow separator localization #1955

ghost opened this issue Oct 11, 2013 · 9 comments
Milestone

Comments

@ghost
Copy link

ghost commented Oct 11, 2013

Created by euromark, 6th Apr 2012. (originally Lighthouse ticket #2763):


What I did

NumberLib::toPercentage(22.11);

What happened

'22.11%'

And since it is a finalized string the separator cannot be adjusted to localized settings anymore (in some countries it is , instead of . here).

What I expected to happen

'22,11%'

Possible Solution

We should provide a way to pass the decimals separator (similar to _numberFormat):

/**
 * Formats a number with a level of precision.
 *
 * `@param` float $number   A floating point number.
 * `@param` integer $precision The precision of the returned number.
 * `@param` string $decimals
 * `@return` float Formatted float.
 * `@link` http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
 */
public static function precision($number, $precision = 3, $decimals = '.') {
    $number = sprintf("%01.{$precision}f", $number);
    if ($decimals != '.') {
        $number = str_replace('.', $decimals, $number);
    }
    return $number;
}

/**
 * Formats a number into a percentage string.
 *
 * `@param` float $number A floating point number
 * `@param` integer $precision The precision of the returned number
 * `@param` string $decimals
 * `@return` string Percentage string
 * `@link` http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
 */
public static function toPercentage($number, $precision = 2, $decimals = '.') {
    return self::precision($number, $precision, $decimals) . '%';
}
@ghost
Copy link
Author

ghost commented Oct 11, 2013

6th Apr 2012, euromark said:


ok, obviously not the best solution since toReadableSize() lacks the same thing.
And that method could even have a "thousands" separator (> 999 TB - I know, pretty unlikely) which my solution doesn't account for.

@ghost
Copy link
Author

ghost commented Oct 11, 2013

7th Apr 2012, euromark said:


we could use number_format() here, as well, I think.

@ghost
Copy link
Author

ghost commented Oct 11, 2013

14th Aug 2012, euromark said:


are there any downsides of using the explicit arguments (similar to CakeNumber::format() etc?)
https://github.com/dereuromark/cakephp/compare/cakephp:2.3...dereuromark:2.3-cakenumber-localized

the advantage would be that we don't have to rely on the current environment settings. they can either be unreliable across systems/plattforms/installations or not desired to be changed inside the app (as they would change other things, as well, and can break existing code, e.g. decimal handling for validation).

@ghost
Copy link
Author

ghost commented Oct 11, 2013

14th Aug 2012, Mark Story said:


It would be painful to have to type ',' every time you have to format a number. Also it changes the existing behavior so it'd have to wait.

@ghost
Copy link
Author

ghost commented Oct 11, 2013

14th Aug 2012, euromark said:


doesnt CakeTime use Configure defaults if none are specified (timezone)? that could be an option to reduce this issue.
I am just curious why the locales are favored and what other experienced so far with them.

@ghost
Copy link
Author

ghost commented Oct 11, 2013

17th Jul 2013, euromark said:


Resolving PR: #1431

@ravage84
Copy link
Member

Shouldn't we change the milestone for this to 3.0 because @dereuromark's PR wasn't merged?

@ravage84
Copy link
Member

Also, those three issues are very similar:
#1965
#1955
#1986

Probably those could be merged into one with a clear, updated goal and possibly a proposal how to implement (taking the discussion of #1431 into account).

@lorenzo
Copy link
Member

lorenzo commented Aug 6, 2014

This was implemented in version 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants