1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[#1] Skript Challenge - Number Format

Discussion in 'Skript Challenge' started by BaeFell, Jan 30, 2017.

?

Do you like Skript Challenges like this?

Poll closed Feb 9, 2017.
  1. Yes

    41 vote(s)
    78.8%
  2. Kinda, they need work

    7 vote(s)
    13.5%
  3. No

    4 vote(s)
    7.7%
Thread Status:
Not open for further replies.
  1. BaeFell

    BaeFell I'm Poppy
    Admin

    Joined:
    Nov 27, 2016
    Messages:
    397
    Likes Received:
    210
    Skript Challenge #1
    Number Format
    Some of you might remember when we've done Skript Challenges before. They were quite fun and showed a different end of the community as people came to show their little tricks of Skript. Skript is a quirky language in that sometimes the logic you put in is not what Skript actually does. This is what makes Skript Challenges so fun!

    Anyways! I'm going to try and do one every now and then, hopefully at least a fortnight apart but sometimes might be weekly. There won't be any set schedule for when I do them, but just keep an eye on this forum section! If you have an idea for a Skript Challenge, please PM me with it and a bit of info on it.

    This Skript Challenge is a number format one! Basically, you must be able to change any length of number into a formatted number so like changing 245863 -> 245,863, 17354682 -> 17,354,682. You can use either "," or ".", depending on what's used locally (even though using "." is wrong lol).

    The rules are as follows (note: rules may change between challenge):
    - Your code must be in [code]<code>[/code] tags so that I can easily read it.
    - You can use addons, but, you're not allowed to use a feature from an addon which does this (don't think any do) and the addon must of been released publicly prior to the creation of this thread. So not, you cannot use a feature like format {_integer} using "," or whatever.
    - You can use another user's code, but please don't edit your messages. I can see all message history.

    Challenge details (specifics about this challenge):
    - The integer you must format is in a variable called "{_integer}".
    - Your code must accept any length of integer and support integers that are less than 3 in length (so like 512).
    - You do not have to message it back to the player

    How answers will be judged:
    The winner will be selected by having the shortest answer. Whitespace won't be added on (so spaces/tabs). Lines count isn't counted either. Just the number of characters. I'll be using http://www.charactercountonline.com/ to judge all answers. Any questions, just ask.

    Just encase you're wondering, I have made this by myself before, and it not only supported any length of integer, but also would automatically pick up the currency sign and if there was a cents part of it. It was for formatting a players balance like $123,456.78.

    Have fun! The closing date is a few days or something. Idk.
     
    • Like Like x 5
  2. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Reserved
     
    • Like Like x 1
  3. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    How I can format my code?
     
  4. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    What?
     
  5. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Code (Skript):
    1. function splitCoins(int: text) :: text:
    2.     set {_Coins} to "%{_int}%"
    3.     set {_length} to length of {_Coins}
    4.     if {_length} >= 3:
    5.         set {_mod} to {_length} mod 3
    6.         if {_mod} is 0:
    7.             set {_groups::*} to groups 1 of {_Coins} matched to "([0-9]{3})"
    8.             set {_Coins} to "%join {_groups::*} with "".""%"
    9.         else:
    10.             set {_1} to the first {_mod} characters of {_Coins}
    11.             set {_2} to subtext of {_Coins} from characters ({_mod}+1) to {_length}
    12.             set {_groups::*} to groups 1 of {_2} matched to "([0-9]{3})"
    13.             set {_Coins} to "%{_1}%.%join {_groups::*} with "".""%"
    14.     return {_Coins}
    15. command /skriptChallenge <integer>:
    16.     trigger:
    17.         set {_anyInt} to arg
    18.         send "Before: %{_anyInt}%"
    19.         set {_anyInt} to splitCoins("%{_anyInt}%")
    20.         send "After: %{_anyInt}%"
     
    #5 ShaneBee, Jan 30, 2017
    Last edited by a moderator: Jan 30, 2017
  6. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Update your post with this:

    [code]
    function splitCoins(int: text) :: text:
    set {_Coins} to "%{_int}%"
    set {_length} to length of {_Coins}
    if {_length} >= 3:
    set {_mod} to {_length} mod 3
    if {_mod} is 0:
    set {_groups::*} to groups 1 of {_Coins} matched to "([0-9]{3})"
    set {_Coins} to "%join {_groups::*} with "".""%"
    else:
    set {_1} to the first {_mod} characters of {_Coins}
    set {_2} to subtext of {_Coins} from characters ({_mod}+1) to {_length}
    set {_groups::*} to groups 1 of {_2} matched to "([0-9]{3})"
    set {_Coins} to "%{_1}%.%join {_groups::*} with "".""%"
    return {_Coins}

    command /skriptChallenge:
    trigger:
    set {_anyInt} to 5000
    send "Before: %{_anyInt}%"
    set {_anyInt} to splitCoins({_anyInt})
    send "After: %{_anyInt}%"
    [/code]
     
    • Agree Agree x 1
  7. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Here's my submission:
    [​IMG]

    607 characters, 97 whitespaces. Do the math.

    Code (Skript):
    1.  
    2. function formatNumber(num: text) :: text:
    3.     set {n::*} to {_num} split at ""
    4.     set {c} to 1
    5.     set {s} to ""
    6.     set {t} to (size of {n::*} / 3) + 1
    7.     if "%{t}%" contains ".67":
    8.         set {d} to 1
    9.     else if "%{t}%" contains ".33":
    10.         set {d} to 3
    11.     else:
    12.         set {d} to 2
    13.     set {t} to rounded down {t}
    14.     loop {n::*}:
    15.         if {d} is more than 0:
    16.             remove 1 from {d}
    17.             set {s} to "%{s}%%loop-value%"
    18.         else if {d} is 0:
    19.             set {s} to "%{s}%,%loop-value%"
    20.             set {d} to -1
    21.             remove 1 from {t}
    22.         else:
    23.             add 1 to {c}
    24.             set {s} to "%{s}%%loop-value%"
    25.             if {c} is 3:
    26.                 {t} is more than 0:
    27.                     remove 1 from {t}
    28.                     set {c} to 0
    29.                     set {s} to "%{s}%,"
    30.  
    Half asleep. So.... GO ME?!
     
    • Like Like x 1
  8. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Can't beat this :emoji_stuck_out_tongue:
     
  9. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Code (Text):
    1.  
    2. function a(b: text) :: text:
    3.   if length of {_b} > 3:
    4.     return "%a(first length of {_b} - 3 characters of {_b})%,%last 3 characters of {_b}%"
    5.   return {_b}
    6.  
    7. command /c <integer>:
    8.   trigger:
    9.     send a("%arg%")
    10.  
    Not sure whether the command was required, but it's there.

    EDIT: Fixed the code formatting issue.
     
    • Like Like x 3
  10. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    You can just do the actual code:
    Code (Text):
    1.  
    2.   # {_b} is {_integer}
    3.   if length of {_b} > 3:
    4.     return "%a(first length of {_b} - 3 characters of {_b})%,%last 3 characters of {_b}%"
    5.   return {_b}
    6.  
     
    • Like Like x 2
    • Useful Useful x 1
  11. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Pretty sure it has to be able to be expanded. :emoji_wink:
     
  12. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    What do you mean by expanded?
     
  13. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Expandable*

    As in, have more than just 6 numbers in it.
    [​IMG]
    Like this
     
  14. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    It's recursive, so it should support numbers of any length of digits:
    [​IMG]
     
    • Like Like x 1
    • Creative Creative x 1
  15. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Well I'll be. Well done
     
    • Like Like x 1
  16. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Returning with proper formated number everytime. Meaning you won't get 100,000, with the comma at the end. It will always be a perfect number 100,000
    Code (Skript):
    1. function numberDecimation(n: number) :: text:
    2.     loop split "%{_n}%" at "":
    3.         add 1 to {_}
    4.         add "%loop-value%" to {_d::*}
    5.         if {_} >= 3:
    6.             set {_} to 0
    7.             add "," to {_d::*}
    8.     set {_s} to size of {_d::*}
    9.     loop integers between 0 and 1:
    10.         set {_} to size of {_d::*} - loop-num
    11.         if {_d::%{_}%} is ",":
    12.             delete {_d::%{_}%}
    13.     return "%join {_d::*} by """"%"
    And if you're ignoring that

    Code (Skript):
    1. function numberDecimation(n: number) :: text:
    2.     loop split "%{_n}%" at "":
    3.         add 1 to {_}
    4.         add "%loop-value%" to {_d::*}
    5.         if {_} >= 3:
    6.             set {_} to 0
    7.             add "," to {_d::*}
    8.     return "%join {_d::*} by """"%"
    And if you want the input to be a text

    Code (Skript):
    1.  
    2. function numberDecimation(t: text) :: text: #Don't know if this line counts
    3.     loop split {_t} at "":
    4.         add 1 to {_}
    5.         add "%loop-value%" to {_d::*}
    6.         if {_} >= 3:
    7.             set {_} to 0
    8.             add "," to {_d::*}
    9.     return "%join {_d::*} by """"%" #Don't know if this line counts
    10.  
     
    #16 ShaneBee, Jan 30, 2017
    Last edited by a moderator: Jan 30, 2017
  17. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Code (Skript):
    1.  
    2. function numFormat(integer: text) :: text:
    3.       return regex replace "(?<=\d)(?=(\d\d\d)+(?!\d))" with "," in {_integer}
    4.  
    I won :emoji_wink:
     
    • Winner x 3
    • Like x 2
    • Creative x 2
    • Funny x 1
    • Useful x 1
  18. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Yeah well. Well. Um. Nobody likes a show off! Pffffft. Regex... Pfffffffft. *not jealous ya know*
     
    • Like Like x 1
  19. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    Hi,
    This is my skript :emoji_stuck_out_tongue:

    Code (Text):
    1.  
    2. function N(i: text) :: text:
    3.    set {_r} to ""
    4.    set {_l} to {_i}' length
    5.    loop {_l} times:
    6.        set {_r} to "%subtext of {_i} from {_l}-loop-value+1 to {_l}-loop-value+1%%{_r}%"
    7.        if loop-value mod 3 is equal to 0:
    8.            if loop-value is not {_l}:
    9.                set {_r} to ".%{_r}%"
    10.    return {_r}
    11.  
     
  20. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    1,691
    Likes Received:
    119
    No one followed this rule except me or I didn't get it.
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.
Loading...