-
Notifications
You must be signed in to change notification settings - Fork 181
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
Add function to format as exponential #652
base: main
Are you sure you want to change the base?
Changes from all commits
6fc64bb
f89d02b
9301e1e
aa7e361
63de49b
c0c20bb
fc432e4
08c72d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,9 @@ formatColumns = function(table, columns, template, ...) { | |
#' # render vapor pressure with only two significant figures. | ||
#' datatable(pressure) %>% formatSignif('pressure',2) | ||
#' | ||
#' # render vapor pressure with only two 2-decimal digits in exponential format. | ||
#' datatable(pressure) %>% formatExp('pressure',2) | ||
#' | ||
#' # apply CSS styles to columns | ||
#' datatable(iris) %>% | ||
#' formatStyle('Sepal.Length', fontWeight = styleInterval(5, c('bold', 'weight'))) %>% | ||
|
@@ -93,6 +96,15 @@ formatSignif = function( | |
formatColumns(table, columns, tplSignif, digits, interval, mark, dec.mark) | ||
} | ||
|
||
|
||
#' @export | ||
#' @rdname formatCurrency | ||
formatExp = function( | ||
table, columns, digits = 2, interval = 3, mark = ',', dec.mark = getOption('OutDec') | ||
) { | ||
formatColumns(table, columns, tplExp, digits, interval, mark, dec.mark) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the arguments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know either, I would sure need to work harder on them, but most likely I will drop them (as I don't know what they do yet). I just copied the code and made a few changes to got it working. |
||
} | ||
|
||
#' @export | ||
#' @rdname formatCurrency | ||
#' @param method the method(s) to convert a date to string in JavaScript; see | ||
|
@@ -210,6 +222,12 @@ tplSignif = function(cols, digits, interval, mark, dec.mark, ...) { | |
) | ||
} | ||
|
||
tplExp = function(cols, digits, interval, mark, dec.mark, ...) { | ||
sprintf( | ||
"DTWidget.formatExp(this, row, data, %d, %d, %d, '%s', '%s');", | ||
cols, digits, interval, mark, dec.mark | ||
) | ||
} | ||
tplDate = function(cols, method, params, ...) { | ||
params = if (length(params) > 0) paste(',', toJSON(params)) else '' | ||
sprintf("DTWidget.formatDate(this, row, data, %d, '%s'%s);", cols, method, params) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,9 @@ | |
var DTWidget = {}; | ||
|
||
// 123456666.7890 -> 123,456,666.7890 | ||
var markInterval = function(d, digits, interval, mark, decMark, precision) { | ||
var markInterval = function(d, digits, interval, mark, decMark, precision, exponential) { | ||
x = precision ? d.toPrecision(digits) : d.toFixed(digits); | ||
if(exponential) {x = exponential ? d.toExponential(digits) : d.toFixed(digits);}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here |
||
if (!/^-?[\d.]+$/.test(x)) return x; | ||
var xv = x.split('.'); | ||
if (xv.length > 2) return x; // should have at most one decimal point | ||
|
@@ -53,6 +54,13 @@ DTWidget.formatSignif = function(thiz, row, data, col, digits, interval, mark, d | |
.html(markInterval(d, digits, interval, mark, decMark, true)); | ||
}; | ||
|
||
DTWidget.formatExp = function(thiz, row, data, col, digits, interval, mark, decMark) { | ||
var d = parseFloat(data[col]); | ||
if (isNaN(d)) return; | ||
$(thiz.api().cell(row, col).node()) | ||
.html(markInterval(d, digits, interval, mark, decMark, false,true)); | ||
}; | ||
|
||
DTWidget.formatDate = function(thiz, row, data, col, method, params) { | ||
var d = data[col]; | ||
if (d === null) return; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a brief description of this function on line 18 above.