reading only the last number from i.e. "1234" (4 that will be)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JaNE

    reading only the last number from i.e. "1234" (4 that will be)

    as 1st, sorry on my clumsy english (now I realy miss some words)

    I have one language specific problem with my site. I would like to tell
    how meny pictures someone have in his or her gallery and I would like to
    have it properly speled (in Croatian). So, I don't want to write:
    "number of pitures in this gallery: 123" rather "John have 123
    pictures"... but, here is my problem, I will write now some little
    Croatian with differences in capital letters:

    1 slikU
    2 slikE
    3 slikE
    4 slikE
    5 slikA
    6 slikA
    7 slikA
    ....
    20 slikA
    21 slikU
    22 slikE
    23 slikE
    24 slikE
    25 sliKA
    ....
    99 slikA
    100 slikA
    101 slikU
    102 slikE...

    so that is pattern:
    all numbers which ends with 1 need to have "U",
    2-4 = "E"
    5-x0 = "A"

    and question: how to read only the last number from count(*) ?

    --
    Jan ko?
    fotografija = zapisano svjetlom | fotozine = foto-e-zin

    --
  • Michael Fesser

    #2
    Re: reading only the last number from i.e. "1234&quot ; (4 that will be)

    .oO(JaNE)
    [color=blue]
    >I have one language specific problem with my site. I would like to tell
    >how meny pictures someone have in his or her gallery and I would like to
    >have it properly speled (in Croatian). So, I don't want to write:
    >"number of pitures in this gallery: 123" rather "John have 123
    >pictures"... but, here is my problem, I will write now some little
    >Croatian with differences in capital letters:
    >[...]
    >
    >so that is pattern:
    >all numbers which ends with 1 need to have "U",
    >2-4 = "E"
    >5-x0 = "A"
    >
    >and question: how to read only the last number from count(*) ?[/color]

    I would do it differently. The pattern is always the same, so it's quite
    easy to just devide the number by 10 and have a look at the remainder
    (modulo operation), e.g.

    $number = 22;
    $foo = array('a', 'u', 'e', 'e', 'e', 'a', 'a', 'a', 'a', 'a');
    print $foo[$number % 10];

    will print 'e'.

    The array $foo contains the chars for 0, 1, 2, ... , 9. The expression

    $number % 10

    calculates the remainder when deviding the number by 10, which is then
    used as an index in the char array.

    HTH
    Micha

    Comment

    • Schraalhans Keukenmeester

      #3
      Re: reading only the last number from i.e. "1234&quot ; (4 that will be)

      JaNE wrote:[color=blue]
      > as 1st, sorry on my clumsy english (now I realy miss some words)
      >
      > I have one language specific problem with my site. I would like to tell
      > how meny pictures someone have in his or her gallery and I would like to
      > have it properly speled (in Croatian). So, I don't want to write:
      > "number of pitures in this gallery: 123" rather "John have 123
      > pictures"... but, here is my problem, I will write now some little
      > Croatian with differences in capital letters:
      >
      > 1 slikU
      > 2 slikE
      > 3 slikE
      > 4 slikE
      > 5 slikA
      > 6 slikA
      > 7 slikA
      > ...
      > 20 slikA
      > 21 slikU
      > 22 slikE
      > 23 slikE
      > 24 slikE
      > 25 sliKA
      > ...
      > 99 slikA
      > 100 slikA
      > 101 slikU
      > 102 slikE...
      >
      > so that is pattern:
      > all numbers which ends with 1 need to have "U",
      > 2-4 = "E"
      > 5-x0 = "A"
      >
      > and question: how to read only the last number from count(*) ?
      >[/color]
      You could also do a typeconversion:

      $str = (string) $num;
      $lastchar = $str [strlen($str)-1];

      followed by an if/else or a switch/case statement, or a lookup using the
      reconverted char in a predefined string/array with all the appropriate
      characters/strings.

      You may omit the explicit conversion, but it is not good coding practice.

      Using modulo as offered by others is probably the neater, quicker and
      more correct approach I admit. The latter bit (the lookup) can be used
      in the modulo approach very well too.

      Schraalhans.

      Comment

      • Chung Leong

        #4
        Re: reading only the last number from i.e. "1234&quot ; (4 that will be)

        "JaNE" <not_me@mail.do t> wrote in message
        news:1gqjgxv.1j lgn2h13rc8wqN%n ot_me@mail.dot. ..[color=blue]
        > as 1st, sorry on my clumsy english (now I realy miss some words)
        >
        > I have one language specific problem with my site. I would like to tell
        > how meny pictures someone have in his or her gallery and I would like to
        > have it properly speled (in Croatian). So, I don't want to write:
        > "number of pitures in this gallery: 123" rather "John have 123
        > pictures"... but, here is my problem, I will write now some little
        > Croatian with differences in capital letters:[/color]

        Use the % operator to get the remainder after a division:

        5 % 10 == 5
        15 % 10 == 5
        135 % 10 == 5
        1234 % 10 == 4

        For the word endings, you probably want to write a function since the logic
        can get quite complicated. And it's something you'd use often.

        An example:

        function k($num) {
        if($num >= 10 && $num <= 19) {
        return 'a';
        }
        $mod = $num % 10;
        if($mod == 1) {
        return 'u';
        }
        else if($mod >= 5) {
        return 'a';
        }
        else {
        return 'e';
        }
        }

        I'm guessing here that it's "0 slika" and not "0 sliku".

        For those who don't understand what the problem is: the OP wants to print
        the correct word ending for a given number of objects. In English, we have
        "1 picture" but "2 pictures". In Slavic languages this is much more
        complicated. 1 is singular; 2, 3, and 4 are plural; 5 and above need a
        different declension (4 pictures, but 5 of pictures). The final digit
        determines the ending (24 pictures, 25 of pictures), which is why the
        modulus of 10 is taken. The exceptions are 10 - 19.


        Comment

        • R. Rajesh Jeba Anbiah

          #5
          [OT] Re: reading only the last number from i.e. &quot;1234&quot ; (4 that will be)

          Chung Leong wrote:
          <snip>[color=blue]
          > I'm guessing here that it's "0 slika" and not "0 sliku".
          >
          > For those who don't understand what the problem is: the OP wants to[/color]
          print[color=blue]
          > the correct word ending for a given number of objects. In English, we[/color]
          have[color=blue]
          > "1 picture" but "2 pictures". In Slavic languages this is much more
          > complicated. 1 is singular; 2, 3, and 4 are plural; 5 and above need[/color]
          a[color=blue]
          > different declension (4 pictures, but 5 of pictures). The final digit
          > determines the ending (24 pictures, 25 of pictures), which is why the
          > modulus of 10 is taken. The exceptions are 10 - 19.[/color]

          Oh cool:) Chung knows Croatian too...

          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

          Comment

          Working...