Where is formatInteger?

1
I use the formatFloat function a lot in MicroflowLabels to format the output. I would like to format integers and long values as well, to get the thousand-separator in (432273 becomes 432.273 or 432,273 depending on the locale). Where is the formatInteger function or what is the right approach to do this? Edit: Btw, I can use a chain of toString, parseFloat and formatFloat calls, but I hope there's an easier solution. Regards, Paul
asked
4 answers
1

Select the integer attribute in your form and look at the Appearance section of the Properties pane. Set the Group digits to 'Yes'

answered
1

Paul,

With some java code there is the option to achieve that what you want. Here is the code I tested to convert the int into a string with the use of digit grouping so that 10000 will become a string 10,000.

package module.actions;

import java.text.DecimalFormat; import com.mendix.systemwideinterfaces.core.UserAction; import com.mendix.systemwideinterfaces.core.IMendixObject;

/**


*/ public class Javaaction extends UserAction<string> { private IMendixObject _inputEntity; private module.proxies.Entity inputEntity;

public Java_action(IMendixObject inputEntity)
{
    super();
    this.__inputEntity = inputEntity;
}

@Override
public String executeAction() throws Exception
{
    this.inputEntity = __inputEntity == null ? null : module.proxies.Entity.initialize(this.getContext(), __inputEntity);

    // BEGIN USER CODE
    String pattern = "###,###.###";
    return customFormat(pattern, (double)inputEntity.getFloorspace());
    // END USER CODE
}

/**
 * Returns a string representation of this action
 */
@Override
public String toString()
{
    return "Java_action";
}

// BEGIN EXTRA CODE
static public String customFormat(String pattern, double value ) {
      DecimalFormat myFormatter = new DecimalFormat(pattern);
      return myFormatter.format(value);
   }
// END EXTRA CODE

} So create a java action in the modeler, copy and paste the code into the java file for the action. Create a MF with an action calling the java action and input the entity (in your example myEntity) and the result is the string with the decimal grouping.

Be aware that this will currently not take the locale of the user into account but with some extra code this can be achieved:

   static public void localizedFormat(String pattern, double value, Locale loc ) {
  NumberFormat nf = NumberFormat.getNumberInstance(loc);
  DecimalFormat df = (DecimalFormat)nf;
  df.applyPattern(pattern);
  return df.format(value);

} For this to work the Locale variable is passed as Locale("en", "US"), this can be retrieve from the CurrentUser.

I hope this helps.

answered
0

Btw, I can use a chain of toString, parseFloat and formatFloat calls, but I hope there's an easier solution.

answered
0

Paul,

With a little effort use the code below to input a integer and a format string to get the formatted result for any integer field. This is now a generic function that can be called with the appropriate fields in the input parameters. So in your example intvalue maps to $MyEntity/Floorspace and the formatString to '###,###.###'.

package module.actions;

import java.text.DecimalFormat; import com.mendix.systemwideinterfaces.core.UserAction;

/**


*/ public class Javaaction2 extends UserAction<string> { private Long intvalue; private String formatstring;

public Java_action_2(Long int_value, String format_string)
{
    super();
    this.int_value = int_value;
    this.format_string = format_string;
}

@Override
public String executeAction() throws Exception
{
    // BEGIN USER CODE
    return customFormat(format_string, (double)int_value);
    // END USER CODE
}

/**
 * Returns a string representation of this action
 */
@Override
public String toString()
{
    return "Java_action_2";
}

// BEGIN EXTRA CODE
static public String customFormat(String pattern, double value ) {
      DecimalFormat myFormatter = new DecimalFormat(pattern);
      return myFormatter.format(value);
   }
// END EXTRA CODE

}

answered