# 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) 1 year 10 months ago #102966

 duvemyster Offline Silver 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 Utilizing number_format leads to an integer with grouped thousands: number_format(NUMBER_CALCULATION) = 2,436. (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.) Adding a "floor" inside "number_format" ensures that it always rounds down: number_format(floor(NUMBER_CALCULATION)) = 2,435. 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. 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) 1 year 10 months ago #103002

 DenisChenu Offline Moderator Lime Posts: 7875 Thank you received: 1077 Karma: 313 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 developpement are on my free time (Say thanks ?). A bug not reported is a bug not corrected. | Please, read the documentation | La doc en français à besoin de vous Official LimeSurvey Partner | Web development | SkeletonQuest, RWD for LimeSurvey The administrator has disabled public write access.

## Utilizing EM to display grouped thousands with trailing zeros (e.g., 2,435.60) 1 year 10 months ago #103258

 duvemyster Offline Silver Donor Posts: 17 Thank you received: 4 Karma: 1 feature request submitted as "Upgrade EM number_format to accept \$decimal". 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) 1 year 10 months ago #103522

 duvemyster Offline Silver 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 Utilizing number_format leads to an integer with grouped thousands: number_format(NUMBER_CALCULATION) = 2,436. 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.) Utilizing sprintf("%.2f", round(NUMBER_CALCULATION,2) - floor(NUMBER_CALCULATION)) gives us the decimals including trailing zeros, though this particular example = 1.00. 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. 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: 1 year 10 months ago by duvemyster. Reason: typos The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.215 seconds