Utilizing EM to display grouped thousands with trailing zeros (e.g., 2,435.60)

More
3 years 9 months ago #102966 by duvemyster
duvemyster created the topic: Utilizing EM to display grouped thousands with trailing zeros (e.g., 2,435.60)
What's an easier way than the following to display grouped thousands with trailing zeros utilizing the Expression Manager (EM)? In the following pieces leading up to the example, NUMBER_CALCULATION = 2435.604321
  1. Utilizing number_format leads to an integer with grouped thousands: number_format(NUMBER_CALCULATION) = 2,436.
  2. (Specifying number_format(NUMBER_CALCULATION,2) with precision would be nice, but that syntax isn't expected to work as it isn't specified in implemented functions .)
  3. Adding a "floor" inside "number_format" ensures that it always rounds down: number_format(floor(NUMBER_CALCULATION)) = 2,435.
  4. Utilizing sprintf("%.2f", round(NUMBER_CALCULATION,2) - floor(NUMBER_CALCULATION)) gives us the decimals including trailing zeros, though it also has a beginning zero = 0.60.
  5. Incorporating "substr" can remove the beginning zero: substr(sprintf("%.2f", round(NUMBER_CALCULATION,2) - floor(NUMBER_CALCULATION)),1,3) = .60.

Then "join" can be employed to string #3 and #5 from above together:

join(number_format(floor(NUMBER_CALCULATION)),substr(sprintf("%.2f", round(NUMBER_CALCULATION,2) - floor(NUMBER_CALCULATION)),1,3)) = 2,435.60


What's a shorter way to accomplish this with existing EM implemented functions?

Please Log in to join the conversation.

More
3 years 9 months ago #103002 by DenisChenu
DenisChenu replied the topic: Utilizing EM to display grouped thousands with trailing zeros (e.g., 2,435.60)
Hi,

Not sure the better way, but :
EM number_format MUST be upgraded to accept $decimals ;).

Please : put a feature request.

Denis

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
3 years 9 months ago #103258 by duvemyster
duvemyster replied the topic: Utilizing EM to display grouped thousands with trailing zeros (e.g., 2,435.60)
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

More
3 years 9 months ago - 3 years 9 months ago #103522 by duvemyster
duvemyster replied the topic: Utilizing EM to display grouped thousands with trailing zeros (e.g., 2,435.60)
Note: There is a flaw in the logic at the top of this post. A trailing .9999 would be added in as .00 without increasing the one's place.

The following corrects this:

Where NUMBER_CALCULATION = 2435.995
  1. Utilizing number_format leads to an integer with grouped thousands: number_format(NUMBER_CALCULATION) = 2,436.
  2. Adding a "floor" inside "number_format" ensures that it always rounds down: number_format(floor(NUMBER_CALCULATION)) = 2,435. (However, we want this rounded up when decimals are .995 - .999 which round up to 1.00.)
  3. Utilizing sprintf("%.2f", round(NUMBER_CALCULATION,2) - floor(NUMBER_CALCULATION)) gives us the decimals including trailing zeros, though this particular example = 1.00.
  4. The one's place would need to be raised in this example but not in instances of 0.00-0.99, so: number_format(floor(NUMBER_CALCULATION + sprintf("%.2f", round(NUMBER_CALCULATION,2) - floor(NUMBER_CALCULATION))) = 2,436.
  5. Incorporating "substr" removes the beginning zero or one: substr(sprintf("%.2f", round(NUMBER_CALCULATION,2) - floor(NUMBER_CALCULATION)),1,3) = .00.


Then "join" can be employed to string #4 and #5 from above together:
join(number_format(floor(NUMBER_CALCULATION + sprintf("%.2f", round(NUMBER_CALCULATION,2) - floor(NUMBER_CALCULATION)))),substr(sprintf("%.2f", round(NUMBER_CALCULATION,2) - floor(NUMBER_CALCULATION)),1,3)) = 2,436.00
Last Edit: 3 years 9 months ago by duvemyster. Reason: typos

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now