Tabulator formatting negative money using accounting principles - tabulator

Love this tool. But using the built-in formatters I can't figure out how to format money when negative with parentheses around it instead of the negative character.
so like (1000) and not -1000
If I need to do something custom I can work with that too.

moneyParen: function money(cell, formatterParams, onRendered) {
var floatVal = parseFloat(cell.getValue()),
number,
integer,
decimal,
rgx;
var decimalSym = formatterParams.decimal || ".";
var thousandSym = formatterParams.thousand || ",";
var symbol = formatterParams.symbol || "";
var after = !!formatterParams.symbolAfter;
var precision = typeof formatterParams.precision !== "undefined" ? formatterParams.precision : 2;
if (isNaN(floatVal)) {
return this.emptyToSpace(this.sanitizeHTML(cell.getValue()));
}
number = precision !== false ? floatVal.toFixed(precision) : floatVal;
number = String(number).split(".");
integer = number[0];
decimal = number.length > 1 ? decimalSym + number[1] : "";
var isNeg = (integer.includes("-"));
if (isNeg === true) { integer = integer.replace("-", ""); }
rgx = /(\d+)(\d{3})/;
while (rgx.test(integer)) {
integer = integer.replace(rgx, "$1" + thousandSym + "$2");
}
var retPt1 = after ? integer + decimal + symbol : symbol + integer + decimal;
return isNeg ? "(" + retPt1 + ")" : retPt1;
},

Thanks, this was very helpful. FYI, you might want to change the last line of the function to keep right justification alignment of positive and negative numbers:
return isNeg ? "(" + retPt1 + ")" : retPt1 + " ";

Related

find wheher string value is integer or decimal kotlin

I have dynamic string value which i get from server response ( eg: a = 18 or a = 18.75) . I need to find whether a value has decimal point or not in kotlin. i need to display it in discount_price
if(product[position].discounted_price.) {
mrp.text = "₹" + product.get(position).price
mrp.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
sellingprice.text = "₹" + product.get(position).discounted_price
tv_cartvariant.text = variant.cart_count.toString()
} else {
mrp.text = "₹" + product.get(position).price + ".00"
mrp.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
sellingprice.text = "₹" + product.get(position).discounted_price + ".00"
tv_cartvariant.text = variant.cart_count.toString()
}
"%.2f".format(price.toDouble()) automatically formats the price with 2 decimal places.
Examples:
"%.2f".format(2.toDouble()) returns 2.00
"%.2f".format(2.36.toDouble()) returns 2.36
"%.2f".format(2.9244.toDouble()) returns 2.92
"%.2f".format(2.1093.toDouble()) returns 2.11
If I understood your question correctly, then try:
if(product[position].discounted_price.toString().contains('.') {
// price has decimal dot
} else {
// price is probably integer
}
It seems that you want to show prices with 2 decimal places, this can be done as following, no need to check for dot
mrp.text = "₹" + "%.2f".format(product.get(position).price.toDouble())
mrp.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
sellingprice.text = "₹" + "%.2f".format(product.get(position).discounted_price.toDouble())
tv_cartvariant.text = variant.cart_count.toString()

How to convert this string 20346017621 in to 20-34601762-1

How we can set up a function on node
After first two number and last number, we want to add - hyphen
able to get a string
like this:xxzzzzzzzzx
and convert in to this:xx-zzzzzzzz-x
example of what we need
function tranformer (xxzzzzzzzzx){
NOT SURE HOW TO SOLVE THIS
return xx-zzzzzzzz-x
}
Thanks we really will appreciate this!
Not idea how to mange this task.
function tranformer(st) {
let newStr = ""
for (let i = 0; i < st.length; i++) {
if (i === 2 || i === st.length - 1) {
newStr += "-"
}
newStr += st[i]
}
return newStr
}
Using Slice
let first = st.slice(0, 2)
let middle = st.slice(2, -1)
let last = st.slice(-1)
newStr = first + "-" + middle + "-" + last
console.log(newStr)
First I would decompose the string into an array, and then use splice command to insert a - char at the specified position:
let str = "xxzzzzzzzzx";
str = str.split('');
str.splice(2, 0, '-'); // insert - at pos 2
str.splice(str.length - 1, 0, '-'); // insert - at pos -1
console.log(str.join('')) //-> xx-zzzzzzzz-x

pretty printing from any format to a desired format

I am wondering if there is a way by which I can convert a format, let's say a tree to a format I want. Take the following example:
a -> b, d
f - > c
f-> v
I want to have this as an output:
a implies (b and d)
f implies (c or v)
This is practically the algorithm that you are looking for, but please take note that this algorithm doesn't support the deep conditions.
function mathLogic(inputs) {
var rows = inputs.split("\n");
for (var i in rows)
rows[i] = rows[i].replace(/\s/g, "");
console.log("Rows: ", rows)
var logics = {};
for (var i in rows) {
var row = rows[i];
var rowSplit = row.split("->");
var from = rowSplit[0];
var to = rowSplit[1];
var ands = to.split(",");
if (!logics[from])
logics[from] = [];
logic = logics[from];
logic.push(ands);
}
for (var i in logics) {
var implyString = i + " implies ";
var orLogic = logics[i];
var ors = [];
for (var j in orLogic) {
var or = orLogic[j]
if (or.length > 1)
ors.push("(" + or.join(" and ") + ")");
else
ors.push(or.join(" and "));
}
if (ors.length > 1)
implyString += "(" + ors.join(" or ") + ")";
else
implyString += ors.join(" or ");
console.log(implyString);
}
}
mathLogic(`a -> b, d
f - > c
f-> v`);

CSV file parsing in C#

Wanted to check if there is any null value in column field in the csv file and also there shouldn't be null value after / in number column, if it is null the entire row should not be written to output file.
name,number,gender,country
iva 1/001 f Antartica
aaju 2/002 m russia
lax 3/ m brazil
ana 4/004 f Thailand
vis 5/005 m
for e.g. 3rd and 5th row should not be written to output file.
using (StreamWriter file = new StreamWriter(filepathop)) {
for (int i = 0; i < csv.Length; i++) {
{
if (i == 0) {
file.WriteLine(header + "," + "num" + "," + "serial" + "," + "date");
}
else {
var newline = new StringBuilder();
string[] words = csv[i].Split(',');
string[] no = words[1].Split('/');
string number = no[0];
string serial = no[1];
newline.Append(number + "," + serial + "," + tokens[0]);
file.WriteLine(csv[i] + "," + newline);
}
}
}
}
}
}
}
You can test for null columns with string.IsNullOrEmpty(column) or column.Length == 0 like so:
if (!string.IsNullOrEmpty(serial) && !string.IsNullOrEmpty(country))
file.WriteLine(csv[i] + "," + newline);
You might want to check and remove white space, too. Depends on your input.

Comparing #Now to a Date/Time field?

How do I compare #Now to a data / time value in a document? This is what I have
var ProjectActiveTo:NotesDateTime = doc.getItemValueDateTimeArray("ProjectActiveTo")[0];
var ProjectExpired;
var d1:Date = #Now();
if (d1 > ProjectActiveTo.toJavaDate())
{
dBar.info("Today: " + d1 + " > " + ProjectActiveTo.toJavaDate());
ProjectExpired = true;
}
else
{
dBar.info("Today: " + d1 + " < " + ProjectActiveTo.toJavaDate());
ProjectExpired = false;
}
But this always seems to return false. I printed out some diagnostic messages.
Today: 1/18/13 6:02 PM < 1/20/01 5:00 PM
Obviously today is greater than 1/20/01 but this is the result of my test. Why?
I have done some searching and saw that the compare member function might be used but it returns an error for me and compare is not in the intelisense (or whatever Lotus calls it) in the design editor.
Found this little snippet on line - it should point you in the right direction
var doc:NotesDocument = varRowView.getDocument();
var d:NotesDateTime = doc.getItemValue("BidDueDate")[0];
var t:NotesDateTime = session.createDateTime("Today");
if (d.timeDifference(t) > 0 ) {
return "Overdue";
}else{
return "";
}

Resources