Forum Discussion
7 Replies
<#assign string = "Best Answer" />
<#assign bestAnswersCount = count />
<#if bestAnswersCount > 1>
<#assign string = "Best Answers" />
</#if>
<#if bestAnswersCount > 999 />
<#assign bestAnswersCount = '1k' />
</#if>
<li class="item">${bestAnswersCount} ${string}</li>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.
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.
How about while loop ?
Any reference or basic coding help me do it more efficient ?
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.
- luk8 years agoBoss
Problem 1) Plural of nouns can also be handled via Lithium Text Strings:
// your lithium string (add in Text Editor in the Studio section) would look // something like this: // custom.best-answer = Best {0,choice,0#Answers|1#Answer|1<Answers} <li class="item">${bestAnswersCount} ${text.format('custom.best-answer', count)}</li>
Problem 2) Shortening of numbers, I usually do this with the follwing function (maybe overkill for your usecase):
<#function math_shorten number> <#local units = [ {"factor": 1, "unit": ""}, {"factor": 1000, "unit": "K"}, {"factor": 1000000, "unit": "M"}, {"factor": 1000000000, "unit": "G"}, {"factor": 1000000000000, "unit": "T"}, {"factor": 1000000000000000, "unit": "P"}, {"factor": 1000000000000000000, "unit": "E"} ] /> <#local order = number?abs?round?c?length /> <#local i = ((order - 1) / 3)?floor /> <#if (i < 0)> <#local i = 0 /> </#if> <#local result = (number / (units[i].factor))?string("0.#") + units[i].unit /> <#return result /> </#function> // you would replace your ${bestAnswersCount} with the following: ${math_shorten(count)} // then you would finally wind up with: <li class="item">${math_shorten(count)} ${text.format('custom.best-answer', count)}</li>
The shortening function is pretty resilient, it will handle negative numbers, cut off a .0 ending etc., the only thing it will break with is if you exceed the maximum possible unit, e.g. you end up asking for an item in the units sequence that does not exists, the likelihood of this happening is very low though...
Related Content
- 12 years ago
- 11 years ago