Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: Strip trailing zeros from a number  (Read 3741 times)

JeremieBourque

  • Newbie
  • *
  • Posts: 2
    • View Profile
Strip trailing zeros from a number
« on: March 18, 2024, 06:43:10 pm »
Hello,

Thank you for creating LimeReport, it is very useful.

I was wondering if it is possible to format a number to a certain precision, but also strip the trailing zeros.

Examples with a precision of 4:
  • 1.0000 -> 1
  • 1.0001 -> 1.0001
  • 1.0100 -> 1.01
  • 1.00001 -> 1

Thanks

Subst

  • Sr. Member
  • ****
  • Posts: 450
    • View Profile
Re: Strip trailing zeros from a number
« Reply #1 on: March 19, 2024, 03:59:45 pm »
The "common" way to format as you want in the script using

Code: [Select]
$S{
  var value=1.000004;
  var approx=numberFormat(value,"f",4);
  while (approx.charAt(approx.length-1)=='0' || approx.charAt(approx.length-1)=='.')
    approx=approx.slice(0,-1);
  approx;
  }