Formatting float type data - need 2 decimals and comma

For some reason I am having difficulty getting the data formatted the way I need it.

in datamapper - I have it set to ‘float’ - I have this because I am also graphing the data
in template - the zero drops off at end of data ( ex. 5750.50 is returned 5750.5)

I do not want the currency symbol. I have tried this setting but it adds parenthesis when it is a negative number. I need to have the negative sign ( ex. -5750.50)

I used toFixed(2) to get the zeros but I can not get the comma added.

Data examples -
Mapper Template Need
-5750.5 -5750.5 -5,750.50
37860.60 37860.6 37,860.60

Any ideas as to why I am having difficulty?
Thanks

Try something like this:

var field = "123456.78";
var result = parseFloat(field).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
logger.info(result);

// output: 123,456.78
1 Like

Alternatively one could use the formatter object with a custom pattern.

var theVal = -5750.5;
var formattedVal = formatter.number( theVal, "#,##0.00" );
//output: -5,750.50

The following shows how (for example) negative values can be placed between parentheses.

var theVal = -5750.5;
var formattedVal = formatter.number( theVal, "#,##0.00;(#,##0.00)" );
//output: (5,750.50)

UPDATE - I think I got it. I used formatter.number(field,“#,###,###;(#,###,###)”). I used all #'s because I have a 0 result sometimes also. I guess now my question is how would I get 0 to return a dash. So if equal to zero return a dash else use above format.


Hi - I use this method all the time. However this time I am using it I am getting an unexpected result. Does anyone know why? What I have is the data type set to float. My data is -763. My result is (0,763). I need it to be (763). I am using - formatter.number(field,“#,##0,000;(#,##0,000)”)

I am using this same structure on other fields and it seems to be fine except for when the data is just hundreds.

Thanks