Forum Discussion
I am learning....Thank you VikasB . The string best answers works awesome. Now i am worried about 1K...2K...2.2K....1L
Thinking about more generic code.
BowBharath I think in rare cases answer count may cross this limit. So you can use it
<#assign string = "Best Answer" /> <#assign bestAnswersCount = count /> <#if bestAnswersCount > 1> <#assign string = "Best Answers" /> <#elseif bestAnswersCount > 999 /> <#assign bestAnswersCount = "1k" /> <#elseif bestAnswersCount > 1999 /> <#assign bestAnswersCount = "2k" /> <#elseif bestAnswersCount > 2999 /> <#assign bestAnswersCount = "3k" /> <#else> <#assign bestAnswersCount = "3k+" /> </#if> <li class="item">${bestAnswersCount} ${string}</li>
LMK If you think it can cross it. If yes, I'll try to find you a better solution.
- TariqHussain8 years agoBoss
VikasB - There could be multiple cases which you can not handle by if else.
e.g
1.2k,1.4k,1.5, 2.3k and so on.
<#assign string = "Best Answer" /> <#assign bestAnswersCount = count /> <#if bestAnswersCount > 1> <#assign string = "Best Answers" /> <#elseif bestAnswersCount > 999 && bestAnswersCount < 1000000 /> <#assign bestAnswersCount = (count/1000)?c + "1K" />
<#else>
<#assign bestAnswersCount = (count/1000000)?c + "1M" /> </#if> <li class="item">${bestAnswersCount} ${string}</li>Above example will work for any number e.g 1500 will be 1.5K, 1500000 will be 1.5M. I have added condition for thousand and millions, you can customize it more if needed.
- BowBharath8 years agoAce
How about while loop ?
Any reference or basic coding help me do it more efficient ?
- VikasB8 years agoBoss
No, looping would not suit you here. Here is the conditional situation so have to go with 'if else'. Switch case could be the better case if there are too many 'if else' but that also would not work here also.
Related Content
- 12 years ago
- 11 years ago