Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
ed3-format-default
Project
Project
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Evgeny
ed3-format-default
Commits
30a48dca
Commit
30a48dca
authored
Aug 11, 2016
by
Дмитрий Никулин
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add basic number and currency formatting
parent
3f24bf4a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
4 deletions
+59
-4
index.js
index.js
+59
-4
No files found.
index.js
View file @
30a48dca
// Default Excel value
const
decimalPlaces
=
2
;
export
default
function
format
(
v
,
type
=
''
,
str
=
''
){
if
(
type
===
''
&&
str
===
''
){
return
basicFormat
(
v
);
}
// TODO: document possible type values somewhere(if not already)?
switch
(
v
.
toLowerCase
()){
case
'number'
:
return
numberFormat
(
v
);
case
'currency'
:
return
currencyFormat
(
v
);
}
}
/**
...
...
@@ -22,14 +33,58 @@ export function getHigherBound(){
return
1
e
+
9
;
};
/**
* Basic auto-formatting for cases when no formatting is set.
* Valid numbers that do not fit into range [abs(lowerBound), abs(higherBound)]
* except zeros will be converted to exponential format.
* @param {*} x - Input value
* @returns {String} String representation of input value
*/
export
function
basicFormat
(
x
){
var
lowerBound
=
getLowerBound
();
var
higherBound
=
getHigherBound
();
let
lowerBound
=
getLowerBound
();
let
higherBound
=
getHigherBound
();
if
(
Math
.
abs
(
x
)
>=
higherBound
||
Math
.
abs
(
x
)
<
lowerBound
&&
Math
.
abs
(
x
)
!==
0
){
return
x
.
toExponential
();
}
return
''
+
x
;
return
x
.
toString
()
;
};
/**
* Basic number formatting, behaves like Excel number formatting with default
* settings (2 decimal places, no 1000 separator)
* @param {*} x - Input value
* @returns {String} String representation of input value
*/
export
function
numberFormat
(
x
){
if
(
isNaN
(
x
)){
throw
new
Error
(
'Unable to format non-numeric and invalid values!'
);
}
else
{
return
parseFloat
(
x
).
toLocaleString
(
undefined
,
// use OS locale
{
minimumFractionDigits
:
decimalPlaces
,
maximumFractionDigits
:
decimalPlaces
}
);
}
}
/**
* Basic currency formatting, behaves like Excel currency formatting with default
* settings (2 decimal places, russian ruble as a currency suffix)
* @param {*} x - Input value
* @returns {String} String representation of input value
*/
export
function
currencyFormat
(
x
){
if
(
isNaN
(
x
)){
throw
new
Error
(
'Unable to format non-numeric and invalid values!'
);
}
else
{
return
parseFloat
(
x
).
toLocaleString
(
undefined
,
{
style
:
'currency'
,
currency
:
'RUR'
}
);
}
}
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