Welcome, Guest
Username: Password: Remember me

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

Utilizing EM to display grouped thousands with trailing zeros (e.g., 2,435.60) 8 months 1 day ago #102966

  • duvemyster
  • duvemyster's Avatar
  • OFFLINE
  • Gold Donor
  • Posts: 17
  • Thank you received: 4
  • Karma: 1
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?
The administrator has disabled public write access.

Utilizing EM to display grouped thousands with trailing zeros (e.g., 2,435.60) 7 months 4 weeks ago #103002

  • DenisChenu
  • DenisChenu's Avatar
  • NOW ONLINE
  • Moderator Lime
  • Posts: 6276
  • Thank you received: 801
  • Karma: 240
Hi,

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

Please : put a feature request.

Denis
The administrator has disabled public write access.

Utilizing EM to display grouped thousands with trailing zeros (e.g., 2,435.60) 7 months 3 weeks ago #103258

  • duvemyster
  • duvemyster's Avatar
  • OFFLINE
  • Gold Donor
  • Posts: 17
  • Thank you received: 4
  • Karma: 1
The administrator has disabled public write access.
The following user(s) said Thank You: DenisChenu

Utilizing EM to display grouped thousands with trailing zeros (e.g., 2,435.60) 7 months 2 weeks ago #103522

  • duvemyster
  • duvemyster's Avatar
  • OFFLINE
  • Gold Donor
  • Posts: 17
  • Thank you received: 4
  • Karma: 1
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: 7 months 2 weeks ago by duvemyster. Reason: typos
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.117 seconds
Donation Image