Audit trail - Number of decimal places

0
Hi all, What is the solution to limiting the number of decimal places displayed in the changed new/old values? Currently it displays it to 8 and a user would like to see 2.  From what I can tell, as this is a String, the change would need to be done within the Java action itself and would require me to amend the code there and not within Mendix. Thanks
asked
3 answers
1

Change the function ‘getValue’ in the ‘CreateLogObject.java’ to:

private static String getValue(IMendixObjectMember<?> member, boolean fromCache, IContext context) {
		Object value = null;
		// Values from cache
		if (fromCache == true)
			value = member.getValue(context);

		// Values form DB
		else
			value = member.getOriginalValue(context);

		if (value != null) {

			if (value instanceof Date)
				return parseDate((Date) value, context);

			else if (value instanceof String)
				return parseString((String) value);
			// 20160825 - Ivo Sturm: added for proper Decimal handling. 
			// In database it is stored with 8 trailing zeros after the comma, in cache it doesn't have, 
			// which incorrectly was creating LogLines...
			else if (value instanceof BigDecimal){
				_logNode.trace("BigDecimal member: " + member.getName() + ", value: " + value);
				String s = String.valueOf(value);
				s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");
				_logNode.trace("BigDecimal member deleted trailing zeros to: " + member.getName() + ", value: " + s);
				return s;
			}				
			return String.valueOf( value ).trim();
		} else
			return "";
}

This will:

  1. prevent the module from incorrectly logging changes on Decimal attributes if the UI is set to 2 decimals. The module always checks with the database with 8 Decimals hence will see every action as a change….
  2. remove any unnecessary trailing zeros from the Decimal field being logged as well as it will
answered
1

 

	private static String getValue(IMendixObjectMember<?> member, boolean fromCache, IContext context) {
		Object value = null;
		// Values from cache
		if (fromCache == true)
			value = member.getValue(context);

		// Values form DB
		else
			value = member.getOriginalValue(context);

		if (value != null) {

			if (value instanceof Date)
				return parseDate((Date) value, context);

			else if (value instanceof String)
				return parseString((String) value);
			// 20160825 - Ivo Sturm: added for proper Decimal handling. 
			// In database it is stored with 8 trailing zeros after the comma, in cache it doesn't have, 
			// which incorrectly was creating LogLines...
			else if (value instanceof BigDecimal){
				_logNode.trace("BigDecimal member: " + member.getName() + ", value: " + value);
				value = ((BigDecimal) value).setScale(2, RoundingMode.CEILING);
				String s = String.valueOf(value);
				_logNode.trace("BigDecimal member deleted trailing zeros to: " + member.getName() + ", value: " + s);
				return s;
			}				
			return String.valueOf( value ).trim();
		} else
			return "";
}

 

answered
1

If you insist you need RoundingMode.CEILING, I would suggest making this an explicit configuration setting. Ceiling is not a sensible default.

answered