Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I find the DecimalFormat class (and Grails's formatNumber tag by extension) a bit opaque for certain use cases, and I still haven't found a reasonable way to do some pretty basic formatting with it without some ugly pre-processing to generate an appropriate format string. I threw together a simple number formatting tag several months ago which essentially constructs a format string and does some minimal processing to the number itself.</p> <p>It's not as generic or elegant as I'd like (it's all we needed at the time - it's super basic, but it still keeps some ugly processing out of GSPs), but it should be easy to read, and it's obvious where it could be trivially improved (i.e. making the scaling iterative instead of naive if-elseif slop, allowing the user to pass in custom scaling markers, allowing for a custom number validator as a parameter, etc.).</p> <pre><code> // Formats a number to 3 significant digits, appending appropriate scale marker // (k, m, b, t, etc.). Defining var allows you to use a string representation // of the formatted number anywhere you need it within the tag body, and // provides the scale as well (in case highlighting or other special formatting // based upon scale is desired). def formatNumberScaled = {attrs, body -> // number, prefix, suffix, invalid, var Double number String numberString String scale try { number = attrs.'number'.toDouble() } catch (Exception e) { number = Double.NaN } if (number.isNaN() || number.isInfinite()) { numberString = scale = attrs.'invalid' ?: "N/A" } else { Boolean negative = number &lt; 0d number = negative ? -number : number if (number &lt; 1000d) { scale = '' } else if (number &lt; 1000000d) { scale = 'k' number /= 1000d } else if (number &lt; 1000000000d) { scale = 'm' number /= 1000000d } else if (number &lt; 1000000000000d) { scale = 'b' number /= 1000000000d } else if (number &lt; 1000000000000000d) { scale = 't' number /= 1000000000000d } String format if (number &lt; 10d) { format = '#.00' } else if (number &lt; 100d) { format = '##.0' } else { format = '###' } format = "'${attrs.'prefix' ?: ''}'${format}'${scale} ${attrs.'suffix' ?: ''}'" numberString = g.formatNumber('number': negative ? -number : number, 'format': format) } // Now, either print the number or output the tag body with // the appropriate variables set if (attrs.'var') { out &lt;&lt; body((attrs.'var'): numberString, 'scale': scale) } else { out &lt;&lt; numberString } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload