Ways of displaying numbers by thousands and millions

1
Hi ! I would like to display number by thousands and millions.  Example: Number : 123,456,789. 32  Expected display : 123 M and 123456 K I cannot find how to do it :( Could you please help me with this? Please without adding any attribute  Thanks a lot!!! Jennifer
asked
3 answers
4

One of the easiest ways may be to add another attribute (String) that always contains a formatted string as you need it. With this solution you can use it in datagrids and wherever you want to use it. But it also means thyt you need to have this string up to date (Calculation events, on change events, calculated attribute,...).

answered
4

It is weird, but this is one of Mendix’s shortcomings. Your request is to have formatting-options kind of like the Custom Formatting of in a Date-attribute in a Datagrid, but that is not available for decimal-attributes.

You do not specify where you want this text to show. If it is in a datagrid, then instead of setting the datasource from database, you can set it from microflow. In the microflow you retrieve the object, modify the value using function formatDecimal() and display that in a separate field, which still is an attribute.

Basically, you either need to have Mendix change the (Datagrid?) widget showing your data, giving more options for formatting, or you need to add an extra attribute. I totally agree that this should not be necessary and adds redundant information with all negative effects.

That said: You can add the attribute in a calculated attribute of non-persistant entity that you relate to your master-entity with a 1-1 relation. I am afraid it does not get better than that for now, allthough I hope someone has a better option available.

answered
2

Hi Jennifer,

This can be accomplished with Javascript. I was able to tweak the cell styler widget created by Eric Tieneber to be able to do this. 

The widget is on my github. You can download the .mpk file and add it to the widgets folder in your projects directory.

https://github.com/austinmcnicholas/CellFormatter/blob/master/dist/CellFormatter.mpk

 

The setup is similar to the cell styler widget so you can also refer to that documentation. 

First fill in the datagrid name and entity type. 

 

The datagrid name can be found here

 

 

Then you can add rules to as many of the columns in the datagrid that you want. For my example I created a datagrid that has a two columns. The first column shows millions, and the second column shows thousands. 

To setup a rule you need to first grab the column name and the attribute name. You can find them here:

 

Then add a rule to your widget and fill it out like this

 

You have the option to use a nano flow to return a string or to write javascript. For this example you want to use javascript.  Also you have a couple options on where to place the string in the column. For this case, use the default value “replace”.

 

The script that you want to use for millions is:

var num = rowObj.get("million"); //million is the attribute name of the column

return Math.abs(num) > 999999 ? Math.sign(num)*((Math.abs(num)/1000000).toFixed(1)) + 'M' : Math.sign(num)*Math.abs(num)

 

and the script for thousands is:

var num = rowObj.get("thousand"); //thousand is the attribute name

return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)

 

Once that is all setup you can run your project and see how the numbers are formatted. Here is a screenshot from my example

 

I also uploaded my test project in case you run into any issues configuring the widget.

https://drive.google.com/file/d/1mCZVmshDanSlsn5GzD47G0vVz8bi0iiA/view?usp=sharing

 

 

Hope this helps!

answered