Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
cbf642d9
Commit
cbf642d9
authored
Jun 04, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for locale-dependent decimal and grouping separators.
parent
5ecb5e3b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
4 deletions
+32
-4
Formatter.php
framework/yii/base/Formatter.php
+12
-4
Formatter.php
framework/yii/i18n/Formatter.php
+20
-0
No files found.
framework/yii/base/Formatter.php
View file @
cbf642d9
...
...
@@ -39,17 +39,19 @@ class Formatter extends Component
public
$datetimeFormat
=
'Y/m/d h:i:s A'
;
/**
* @var array the text to be displayed when formatting a boolean value. The first element corresponds
* to the text display for false, the second element for true. Defaults to
<code>array('No', 'Yes')</code>
.
* to the text display for false, the second element for true. Defaults to
`array('No', 'Yes')`
.
*/
public
$booleanFormat
;
/**
* @var string the character displayed as the decimal point when formatting a number.
* If not set, "." will be used.
*/
public
$decimalSeparator
=
'.'
;
public
$decimalSeparator
;
/**
* @var string the character displayed as the thousands separator character when formatting a number.
* If not set, "," will be used.
*/
public
$thousandSeparator
=
','
;
public
$thousandSeparator
;
/**
...
...
@@ -273,8 +275,12 @@ class Formatter extends Component
*/
public
function
asDouble
(
$value
,
$decimals
=
2
)
{
if
(
$this
->
decimalSeparator
===
null
)
{
return
sprintf
(
"%.
{
$decimals
}
f"
,
$value
);
}
else
{
return
str_replace
(
'.'
,
$this
->
decimalSeparator
,
sprintf
(
"%.
{
$decimals
}
f"
,
$value
));
}
}
/**
* Formats the value as a number with decimal and thousand separators.
...
...
@@ -287,6 +293,8 @@ class Formatter extends Component
*/
public
function
asNumber
(
$value
,
$decimals
=
0
)
{
return
number_format
(
$value
,
$decimals
,
$this
->
decimalSeparator
,
$this
->
thousandSeparator
);
$ds
=
isset
(
$this
->
decimalSeparator
)
?
$this
->
decimalSeparator
:
'.'
;
$ts
=
isset
(
$this
->
thousandSeparator
)
?
$this
->
thousandSeparator
:
','
;
return
number_format
(
$value
,
$decimals
,
$ds
,
$ts
);
}
}
framework/yii/i18n/Formatter.php
View file @
cbf642d9
...
...
@@ -54,6 +54,16 @@ class Formatter extends \yii\base\Formatter
* creating a new number formatter to format decimals, currencies, etc.
*/
public
$numberFormatOptions
=
array
();
/**
* @var string the character displayed as the decimal point when formatting a number.
* If not set, the decimal separator corresponding to [[locale]] will be used.
*/
public
$decimalSeparator
;
/**
* @var string the character displayed as the thousands separator character when formatting a number.
* If not set, the thousand separator corresponding to [[locale]] will be used.
*/
public
$thousandSeparator
;
/**
...
...
@@ -70,6 +80,16 @@ class Formatter extends \yii\base\Formatter
if
(
$this
->
locale
===
null
)
{
$this
->
locale
=
Yii
::
$app
->
language
;
}
if
(
$this
->
decimalSeparator
===
null
||
$this
->
thousandSeparator
===
null
)
{
$formatter
=
new
NumberFormatter
(
$this
->
locale
,
NumberFormatter
::
DECIMAL
);
if
(
$this
->
decimalSeparator
===
null
)
{
$this
->
decimalSeparator
=
$formatter
->
getSymbol
(
NumberFormatter
::
GROUPING_SEPARATOR_SYMBOL
);
}
if
(
$this
->
thousandSeparator
===
null
)
{
$this
->
thousandSeparator
=
$formatter
->
getSymbol
(
NumberFormatter
::
DECIMAL_SEPARATOR_SYMBOL
);
}
}
parent
::
init
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment