Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Can I fix the decimals while rounding?

  • LSWipo
  • LSWipo's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 5 months ago #223719 by LSWipo
Please help us help you and fill where relevant:
Your LimeSurvey version: LimeSurvey 3.0
Own server or LimeSurvey hosting: 
Theme: 
test_theme
==================
Hello,

I am trying to round the values with two decimals but when the last digit is rounded to zero, zero does not show up. I need every number to be rounded with two decimals even if the last digit is zero. I could not find the code for that. Please see the code that I use below: 

fixnum(round(variable, 2))

I used fixnum because I need to have a comma other than a dot while presenting it.  As an example: It turns "27,17" if the number is "27,167826" but turns "27,1" if the number is "27,102153" while I need it to be "27,10" if possible. 

I will appreciate if you could help.

 
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #223722 by Joffm
Replied by Joffm on topic Can I fix the decimals while rounding?
Hm,
yes, that's mathematics.

So, you may do some string manipulations like
{if(strpos(fixnum(round(Q1,2)),'.')==0,join(fixnum(round(Q1,2)),'.00'),if(strpos(fixnum(round(Q1,2)),'.')==strlen(fixnum(round(Q1,2)))-2,join(fixnum(round(Q1,2)),'0'),fixnum(round(Q1,2))))}

The long version.
If you calculate the fixnum part in a separate equation (eqfix), it is
{if(strpos(eqfix,'.')==0,join(eqfix,'.00'),if(strpos(eqfix,'.')==strlen(eqfix)-2,join(eqfix,'0'),eqfix)))}

You have to adapt comma vs. dot.


 






Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • LSWipo
  • LSWipo's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 5 months ago #223723 by LSWipo
Replied by LSWipo on topic Can I fix the decimals while rounding?
Thank you so much for the quick reply.

Please help us help you and fill where relevant:
Your LimeSurvey version: LimeSurvey 3.0
Own server or LimeSurvey hosting: 
Theme: test_theme
==================
Hello,

How I should manipulate the code to have it rounded, such that there is only one true decimal, and the second decimal is zero. Hence, if the number drawn is "30,232", the page should display “30,20”. If "30,252" is drawn, it should display “30,30”?
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #223730 by Joffm
Replied by Joffm on topic Can I fix the decimals while rounding?
Round to one decimal.

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #223732 by holch
Replied by holch on topic Can I fix the decimals while rounding?

Round to one decimal.


Hahahaham, I don't think that will solve his or her problem.

They want x.xx in any case, even if it is 1.70 (not 1.7).

If they round to one decimal, the same can happen, only 1.0 would turn to 1.

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago - 3 years 5 months ago #223733 by Joffm
Replied by Joffm on topic Can I fix the decimals while rounding?
Oh, no
you see the initial requirement was to have two decimals always.
LSWipo used the rounding fixnum(round(Q1,2))
And I proposed the solution
{if(strpos(fixnum(round(Q1,2)),'.')==0,join(fixnum(round(Q1,2)),'.00'),if(strpos(fixnum(round(Q1,2)),'.')==strlen(fixnum(round(Q1,2)))-2,join(fixnum(round(Q1,2)),'0'),fixnum(round(Q1,2))))}

Now there was the next requirement

that there is only one true decimal, and the second decimal is zero

This is covered by doing the rounding to only one decimal. The displayed number of decimals is done by the string manipulation.
{if(strpos(fixnum(round(Q1,1)),'.')==0,join(fixnum(round(Q1,1)),'.00'),if(strpos(fixnum(round(Q1,1)),'.')==strlen(fixnum(round(Q1,1)))-2,join(fixnum(round(Q1,1)),'0'),fixnum(round(Q1,1))))}

But now I see that my screenshots were missing.

 



Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 5 months ago by Joffm.
The topic has been locked.
  • LSWipo
  • LSWipo's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 5 months ago #223734 by LSWipo
Replied by LSWipo on topic Can I fix the decimals while rounding?
Thank you so much, this was very helpful
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose