LimeReport Forum

General Category | Основное => Discussion | Обсуждение => Topic started by: Paul Traut on December 15, 2017, 12:00:12 PM

Title: Text formating
Post by: Paul Traut on December 15, 2017, 12:00:12 PM
Hello,
I would like to use a "counter" within a text field like
1st
2nd
3rd
4th
...
21st
22nd
23rd
24th
...
and so on.
Is there a possibility to do this in a way, that makes sure that the 2718th entry is also formatted correct?

Paul
Title: Re: Text formating
Post by: Paul Traut on December 15, 2017, 12:06:24 PM
PS.: How can I format a text within LimeReport to get something like this "HelloHello "???

Paul
Title: Re: Text formating
Post by: Arin Alex on December 15, 2017, 01:12:23 PM
Hi
You can write something like this in the init script
Code: [Select]
function formatNumber(number){
  var lastNumber = number.toString().charAt(number.toString().length-1);
  switch(lastNumber){
      case "1":
         result = "<sup>st</sup>"
      break;
      case "2":
          result = "<sup>nd</sup>"
      break;
      ......
  }
  return number.toString()+result;
}
And use it in a TextItem with AlloHTML
Code: [Select]
$S{formatNumber(12)}
Title: Re: Text formating
Post by: Paul Traut on December 15, 2017, 02:35:59 PM
Ok, thanks.

But this means, I have to write within the script a case for every single number that I could expect ???
Title: Re: Text formating
Post by: Paul Traut on December 15, 2017, 03:50:11 PM
Hello Alex,
this code doesent work right, I get
1<sup>st</sup>
2<sup>nd</sup>
...

as a result and not
1st
2nd
...
Title: Re: Text formating
Post by: Arin Alex on December 15, 2017, 03:56:55 PM
Case should process only the last digit in the number.
Do you turn on AllowHTML at TextItem?
Title: Re: Text formating
Post by: Paul Traut on December 15, 2017, 04:04:40 PM
Oh
I have over read this important information, now it works  ;D Thanks.

Can I also use the MOD operator within the script, than it would be easier to write a script for an unknown amount of lines
 :)
Title: Re: Text formating
Post by: Fynjy on December 18, 2017, 10:12:56 AM
Hi
You can write something like this in the init script
Code: [Select]
function formatNumber(number){
  var lastNumber = number.toString().charAt(number.toString().length-1);
  switch(lastNumber){
      case "1":
         result = "<sup>st</sup>"
      break;
      case "2":
          result = "<sup>nd</sup>"
      break;
      ......
  }
  return number.toString()+result;
}
А куда этот код запихивать? Туплю немного)
Title: Re: Text formating
Post by: Paul Traut on December 18, 2017, 11:23:36 AM
Hello Fynjy,
unfortunately, I can not read Russian  :(
Title: Re: Text formating
Post by: Subst on December 18, 2017, 11:31:44 AM
Paul, the question by Fynjy about the init script location in LMDesigner...

Fynjy: смотри файл, там стрелкой указано, где инит скрипт располагать
Title: Re: Text formating
Post by: Arin Alex on December 18, 2017, 12:11:03 PM
Paul,
Quote
Can I also use the MOD operator within the script, than it would be easier to write a script for an unknown amount of lines
Limereport uses javascript. In javascrip Modulus (%) operator returns only the remainder. 
Title: Re: Text formating
Post by: Paul Traut on December 18, 2017, 02:39:11 PM
Paul, the question by Fynjy about the init script location in LMDesigner...

Fynjy: смотри файл, там стрелкой указано, где инит скрипт располагать
Ok, Thanks

Paul,
Quote
Can I also use the MOD operator within the script, than it would be easier to write a script for an unknown amount of lines
Limereport uses javascript. In javascrip Modulus (%) operator returns only the remainder. 

Thanks, will use this
Title: Re: Text formating
Post by: Paul Traut on December 18, 2017, 04:43:00 PM
Ok,

here is my solution. Maybe someone is searching for the same in the future
 :)
Code: [Select]
function formatNumber(number)
{
    var modNumber = number % 10;
    switch(modNumber)
    {
        case 1 :
        {
             tensDigit = number.toString().charAt(number.toString().length-2);
             switch(tensDigit)
            {
                 case "1":
                     result = "<sup>th</sup>"
                 break;
                 default:
                     result = "<sup>st</sup>"
                 break;
            }
        }
      break;
      case 2 :
          result = "<sup>nd</sup>" 
      break;
      case 3 :
          result = "<sup>rd</sup>"
      break;
      default:
          result = "<sup>th</sup>"
      break;
    }
    return number.toString()+result;
}