Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
8b52d7b5
Commit
8b52d7b5
authored
6 years ago
by
Tom Robinson
Browse files
Options
Downloads
Patches
Plain Diff
Fix currency formatting of negative numbers when currency_in_header = true
parent
484a55c3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
frontend/src/metabase/lib/formatting.js
+3
-2
3 additions, 2 deletions
frontend/src/metabase/lib/formatting.js
frontend/test/lib/formatting.unit.spec.js
+38
-0
38 additions, 0 deletions
frontend/test/lib/formatting.unit.spec.js
with
41 additions
and
2 deletions
frontend/src/metabase/lib/formatting.js
+
3
−
2
View file @
8b52d7b5
...
...
@@ -124,7 +124,8 @@ const getDayFormat = options =>
const
RANGE_SEPARATOR
=
` – `
;
// for extracting number portion from a formatted currency string
const
NUMBER_REGEX
=
/
[\+\-]?[
0-9
\.
,
]
+/
;
// NOTE: match minus/plus and number separately to handle interposed currency symbol -$1.23
const
NUMBER_REGEX
=
/
([\+\-])?[^
0-9
]
*
([
0-9
\.
,
]
+
)
/
;
const
DEFAULT_NUMBER_SEPARATORS
=
"
.,
"
;
...
...
@@ -190,7 +191,7 @@ export function formatNumber(number: number, options: FormattingOptions = {}) {
)
{
const
match
=
formatted
.
match
(
NUMBER_REGEX
);
if
(
match
)
{
formatted
=
match
[
0
]
.
trim
();
formatted
=
(
match
[
1
]
||
""
).
trim
()
+
(
match
[
2
]
||
""
)
.
trim
();
}
}
...
...
This diff is collapsed.
Click to expand it.
frontend/test/lib/formatting.unit.spec.js
+
38
−
0
View file @
8b52d7b5
...
...
@@ -52,6 +52,44 @@ describe("formatting", () => {
expect
(
formatNumber
(
1.111
)).
toEqual
(
"
1.11
"
);
expect
(
formatNumber
(
111.111
)).
toEqual
(
"
111.11
"
);
});
describe
(
"
number_style = currency
"
,
()
=>
{
it
(
"
should handle positive currency
"
,
()
=>
{
expect
(
formatNumber
(
1.23
,
{
number_style
:
"
currency
"
,
currency
:
"
USD
"
}),
).
toBe
(
"
$1.23
"
);
});
it
(
"
should handle negative currency
"
,
()
=>
{
expect
(
formatNumber
(
-
1.23
,
{
number_style
:
"
currency
"
,
currency
:
"
USD
"
}),
).
toBe
(
"
-$1.23
"
);
});
describe
(
"
with currency_in_header = true and type = cell
"
,
()
=>
{
it
(
"
should handle positive currency
"
,
()
=>
{
expect
(
formatNumber
(
1.23
,
{
number_style
:
"
currency
"
,
currency
:
"
USD
"
,
currency_in_header
:
true
,
type
:
"
cell
"
,
}),
).
toBe
(
"
1.23
"
);
});
it
(
"
should handle negative currency
"
,
()
=>
{
expect
(
formatNumber
(
-
1.23
,
{
number_style
:
"
currency
"
,
currency
:
"
USD
"
,
currency_in_header
:
true
,
type
:
"
cell
"
,
}),
).
toBe
(
"
-1.23
"
);
});
});
});
});
describe
(
"
formatValue
"
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment