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
8b644787
Commit
8b644787
authored
May 29, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored numberformatting in base\Formatter
added decimalSeparator and thousandSeparator properties. issue #48
parent
01c20996
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
6 deletions
+37
-6
Formatter.php
framework/yii/base/Formatter.php
+15
-5
FormatterTest.php
tests/unit/framework/base/FormatterTest.php
+22
-1
No files found.
framework/yii/base/Formatter.php
View file @
8b644787
...
@@ -42,6 +42,14 @@ class Formatter extends Component
...
@@ -42,6 +42,14 @@ class Formatter extends Component
* 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 <code>array('No', 'Yes')</code>.
*/
*/
public
$booleanFormat
;
public
$booleanFormat
;
/**
* @var string the character displayed as the decimal point when formatting a number.
*/
public
$decimalSeparator
=
'.'
;
/**
* @var string the character displayed as the thousands separator character when formatting a number.
*/
public
$thousandSeparator
=
','
;
/**
/**
...
@@ -257,13 +265,15 @@ class Formatter extends Component
...
@@ -257,13 +265,15 @@ class Formatter extends Component
/**
/**
* Formats the value as a double number.
* Formats the value as a double number.
* Property [[decimalSeparator]] will be used to represent the decimal point.
* @param mixed $value the value to be formatted
* @param mixed $value the value to be formatted
* @param integer $decimals the number of digits after the decimal point
* @param integer $decimals the number of digits after the decimal point
* @return string the formatting result.
* @return string the formatting result.
* @see decimalSeparator
*/
*/
public
function
asDouble
(
$value
,
$decimals
=
2
)
public
function
asDouble
(
$value
,
$decimals
=
2
)
{
{
return
s
printf
(
"%.
{
$decimals
}
f"
,
$value
);
return
s
tr_replace
(
'.'
,
$this
->
decimalSeparator
,
sprintf
(
"%.
{
$decimals
}
f"
,
$value
)
);
}
}
/**
/**
...
@@ -271,12 +281,12 @@ class Formatter extends Component
...
@@ -271,12 +281,12 @@ class Formatter extends Component
* This method calls the PHP number_format() function to do the formatting.
* This method calls the PHP number_format() function to do the formatting.
* @param mixed $value the value to be formatted
* @param mixed $value the value to be formatted
* @param integer $decimals the number of digits after the decimal point
* @param integer $decimals the number of digits after the decimal point
* @param string $decimalSeparator the character displayed as the decimal point
* @param string $thousandSeparator the character displayed as the thousands separator character.
* @return string the formatted result
* @return string the formatted result
* @see decimalSeparator
* @see thousandSeparator
*/
*/
public
function
asNumber
(
$value
,
$decimals
=
0
,
$decimalSeparator
=
'.'
,
$thousandSeparator
=
','
)
public
function
asNumber
(
$value
,
$decimals
=
0
)
{
{
return
number_format
(
$value
,
$decimals
,
$
decimalSeparator
,
$
thousandSeparator
);
return
number_format
(
$value
,
$decimals
,
$
this
->
decimalSeparator
,
$this
->
thousandSeparator
);
}
}
}
}
tests/unit/framework/base/FormatterTest.php
View file @
8b644787
...
@@ -140,6 +140,10 @@ class FormatterTest extends TestCase
...
@@ -140,6 +140,10 @@ class FormatterTest extends TestCase
$this
->
assertSame
(
"123"
,
$this
->
formatter
->
asInteger
(
$value
));
$this
->
assertSame
(
"123"
,
$this
->
formatter
->
asInteger
(
$value
));
$value
=
'a'
;
$value
=
'a'
;
$this
->
assertSame
(
"0"
,
$this
->
formatter
->
asInteger
(
$value
));
$this
->
assertSame
(
"0"
,
$this
->
formatter
->
asInteger
(
$value
));
$value
=
-
123.23
;
$this
->
assertSame
(
"-123"
,
$this
->
formatter
->
asInteger
(
$value
));
$value
=
"-123abc"
;
$this
->
assertSame
(
"-123"
,
$this
->
formatter
->
asInteger
(
$value
));
}
}
public
function
testAsDouble
()
public
function
testAsDouble
()
...
@@ -147,12 +151,29 @@ class FormatterTest extends TestCase
...
@@ -147,12 +151,29 @@ class FormatterTest extends TestCase
$value
=
123.12
;
$value
=
123.12
;
$this
->
assertSame
(
"123.12"
,
$this
->
formatter
->
asDouble
(
$value
));
$this
->
assertSame
(
"123.12"
,
$this
->
formatter
->
asDouble
(
$value
));
$this
->
assertSame
(
"123.1"
,
$this
->
formatter
->
asDouble
(
$value
,
1
));
$this
->
assertSame
(
"123.1"
,
$this
->
formatter
->
asDouble
(
$value
,
1
));
$this
->
assertSame
(
"123"
,
$this
->
formatter
->
asDouble
(
$value
,
0
));
$value
=
123
;
$this
->
assertSame
(
"123.00"
,
$this
->
formatter
->
asDouble
(
$value
));
$this
->
formatter
->
decimalSeparator
=
','
;
$value
=
123.12
;
$this
->
assertSame
(
"123,12"
,
$this
->
formatter
->
asDouble
(
$value
));
$this
->
assertSame
(
"123,1"
,
$this
->
formatter
->
asDouble
(
$value
,
1
));
$this
->
assertSame
(
"123"
,
$this
->
formatter
->
asDouble
(
$value
,
0
));
$value
=
123123.123
;
$this
->
assertSame
(
"123123,12"
,
$this
->
formatter
->
asDouble
(
$value
));
}
}
public
function
testAsNumber
()
public
function
testAsNumber
()
{
{
$value
=
123123.123
;
$value
=
123123.123
;
$this
->
assertSame
(
"123,123"
,
$this
->
formatter
->
asNumber
(
$value
));
$this
->
assertSame
(
"123,123"
,
$this
->
formatter
->
asNumber
(
$value
));
$this
->
assertSame
(
"123.123,12"
,
$this
->
formatter
->
asNumber
(
$value
,
2
,
','
,
'.'
));
$this
->
assertSame
(
"123,123.12"
,
$this
->
formatter
->
asNumber
(
$value
,
2
));
$this
->
formatter
->
decimalSeparator
=
','
;
$this
->
formatter
->
thousandSeparator
=
' '
;
$this
->
assertSame
(
"123 123"
,
$this
->
formatter
->
asNumber
(
$value
));
$this
->
assertSame
(
"123 123,12"
,
$this
->
formatter
->
asNumber
(
$value
,
2
));
$this
->
formatter
->
thousandSeparator
=
''
;
$this
->
assertSame
(
"123123"
,
$this
->
formatter
->
asNumber
(
$value
));
$this
->
assertSame
(
"123123,12"
,
$this
->
formatter
->
asNumber
(
$value
,
2
));
}
}
}
}
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