Welcome to the LimeSurvey Community Forum

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

Rounding three-digit numbers with two decimals?

  • LSWipo
  • LSWipo's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 3 months ago #224309 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 numbers with just one real decimal and the last digit being zero. For example, when the number is "29.2869" is supposed to be rounded to "29,30" or when "29.0156" to "29,00". This rounding is only required when unit 3 is selected among three options. The variable Q1 is produced by multiplying a number determined by the participant and another randomly chosen number among 100 options, but is coded in another part.The code I use for rounding is as follows:

{if(Unit == 1, round(Q1*52*Hours,0), if(Unit == 2, round(Q1*4.33*Hours,0),(if(strpos(fixnum(round(Q1,1)),'.') == strlen(fixnum(round(Q1,1)))-2, join(fixnum(round(Q,1)),',00'), join(fixnum(round(Q1,1)), '0')))))} {Unit.shown}

It works perfectly fine when the number is two-digit in any case. It also works for one-digit and three-digit when the last digit is not zero. However, when the number is three-digit and rounded with the first decimal zero, it does not work. For example, for "101.0106" it gives an output of "1010". And also for one-digit, I face a very similar problem. For example when the number is "9.8023", it produces an output of "9,8,00".

I really cannot find a way to solve this problem. 

I will appreciate if you could help. 
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #224316 by Joffm
Hi,
I only can say: If you do not, what you are told, you can't expect a correct result:
In the previous post about this I showed you:
{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))))}

Now combined with the IF of the units it is:
{if(Unit == 1, round(Q1*52*Hours,0), if(Unit == 2, round(Q1*4.33*Hours,0), 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))))))}

If you compare with your try you see that some parts are missing:
{if(Unit == 1, round(Q1*52*Hours,0), if(Unit == 2, round(Q1*4.33*Hours,0),(if(strpos(fixnum(round(Q1,1)),'.') == strlen(fixnum(round(Q1,1)))-2, join(fixnum(round(Q ,1)),',00'), join(fixnum(round(Q1,1)), '0')))))}

I assume this big red "Q" was a typo. But even if I replace by "Q1" your equation does not work.

 
See here the comparison of your equation and mine:







Joffm

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
2 years 3 months ago #224349 by LSWipo
Hello. Thank you so much for your responses so far. I implied the code as you suggested before but it did not work as I wanted, so I modified it by using the trial and error method. Now, I exactly implemented the code again but it is still not functioning for me. I really do not know what I am doing wrong. The exact code is as follows:

{if(Unit == 1, round(Q1*52*Hours,0), if(Unit == 2, round(Q1*4.33*Q1,0),(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)))))))}{Unit.shown}
Hours and Q1 variables are randomly chosen depending on the previous responses of the respondent.

The code produced outcomes like "114,8,00", "126,00"

I will really appreciate if you could help further.
The topic has been locked.
  • LSWipo
  • LSWipo's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 3 months ago #224350 by LSWipo
Sorry, the code is:

€ {if(Unit == 1, round(Q1*52*Hours,0), if(Unit == 2, round(Q1*4.33*Hours,0),(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)))))))}{Unit.shown}
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago - 2 years 3 months ago #224357 by Joffm
Send your lss export.

And did you consider, what I wrote in my very first post about this?

You have to adapt comma vs. dot.

It is obviously something like that.
See here:
 

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 2 years 3 months ago by Joffm.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #224363 by Joffm
Now I could reproduce it.

If you set "comma" as decimal separator, it doesn't work - as I said before.
But as you write here in the English part I used a "dot" as separator.

You have to adapt this.

Joffm

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
2 years 3 months ago #224365 by LSWipo
Hello,

In the text elements, the comma is chosen as the decimal mark. Does this mean that I should change it to dot or do I totally misunderstand? Sorry, I am new to LimeSurvey, and sometimes it is too complicated for someone who has just started to learn.
The topic has been locked.
  • LSWipo
  • LSWipo's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 3 months ago #224366 by LSWipo
And where should I send lss export?
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #224369 by Joffm
You attach it here.

But did you check the possible dot - comma issue?

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
2 years 3 months ago #224370 by LSWipo
I am not sure if I understand what I needed to correct with the dot and comma. Can I send you a private message for the lss export?
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #224373 by Joffm
There is a setting in "text elements" to select the decimal separator.

And in the function
if(strpos(fixnum(round(Q1,1)),'.') == 0
you see that I test for a dot in the number.
If you use a comma the strpos function will always give a result of "0", "not found".

Later I joined the string with a comma
join(fixnum(round(Q1,1)),',00')
I do not remember why (maybe because I'm German).

These things you have to check.

If you had sent a lss export of your tries we would have seen your settings.
Only a verbatim explanation is sometimes like looking into the crystal ball.

Joffm

 

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
2 years 3 months ago #224378 by LSWipo
Thank you so much!

I have the comma as a decimal mark, and the numbers needed to be presented with a comma in between because it is a German experiment.

Now, replaced the '.' with ',' and suddenly it started to work for every digit!

Thank you so much again!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose