[#1] Skript Challenge - Number Format

  • 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!

Do you like Skript Challenges like this?

  • Yes

    Votes: 41 78.8%
  • Kinda, they need work

    Votes: 7 13.5%
  • No

    Votes: 4 7.7%

  • Total voters
    52
  • Poll closed .
Status
Not open for further replies.

BaeFell

I'm Poppy
Staff member
Admin
skUnity Legend
Nov 27, 2016
1,022
241
73
discord.gg
Discord Username
BaeFell
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.
 
code_language.skript:
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 <integer>:
    trigger:
        set {_anyInt} to arg
        send "Before: %{_anyInt}%"
        set {_anyInt} to splitCoins("%{_anyInt}%")
        send "After: %{_anyInt}%"
 
Last edited by a moderator:
Look at my 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}%"

You know that this code isn't "formatted"/not correct (spaces)
How I can "correct" posting my code?
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]
 
Here's my submission:
cQkZxeQ.png


607 characters, 97 whitespaces. Do the math.

code_language.skript:
function formatNumber(num: text) :: text:
    set {n::*} to {_num} split at ""
    set {c} to 1
    set {s} to ""
    set {t} to (size of {n::*} / 3) + 1
    if "%{t}%" contains ".67":
        set {d} to 1
    else if "%{t}%" contains ".33":
        set {d} to 3
    else:
        set {d} to 2
    set {t} to rounded down {t}
    loop {n::*}:
        if {d} is more than 0:
            remove 1 from {d}
            set {s} to "%{s}%%loop-value%"
        else if {d} is 0:
            set {s} to "%{s}%,%loop-value%"
            set {d} to -1
            remove 1 from {t}
        else:
            add 1 to {c}
            set {s} to "%{s}%%loop-value%"
            if {c} is 3:
                {t} is more than 0:
                    remove 1 from {t}
                    set {c} to 0
                    set {s} to "%{s}%,"

Half asleep. So.... GO ME?!
 
  • Like
Reactions: Getreidemonster
Code:
function a(b: text) :: text:
  if length of {_b} > 3:
    return "%a(first length of {_b} - 3 characters of {_b})%,%last 3 characters of {_b}%"
  return {_b}

command /c <integer>:
  trigger:
    send a("%arg%")

Not sure whether the command was required, but it's there.

EDIT: Fixed the code formatting issue.
 
Code:
function a(b: text) :: text:
  if length of {_b} > 3:
    return "%a(first length of {_b} - 3 characters of {_b})%,%last 3 characters of {_b}%"
  return {_b}

command /c <integer>:
  trigger:
    send a("%arg%")

Not sure whether the command was required, but it's there.

EDIT: Fixed the code formatting issue.
You can just do the actual code:
Code:
  # {_b} is {_integer}
  if length of {_b} > 3:
    return "%a(first length of {_b} - 3 characters of {_b})%,%last 3 characters of {_b}%"
  return {_b}
 
Code:
function a(b: text) :: text:
  if length of {_b} > 3:
    return "%a(first length of {_b} - 3 characters of {_b})%,%last 3 characters of {_b}%"
  return {_b}

command /c <integer>:
  trigger:
    send a("%arg%")

Not sure whether the command was required, but it's there.

EDIT: Fixed the code formatting issue.
Pretty sure it has to be able to be expanded. :emoji_wink:
 
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_language.skript:
function numberDecimation(n: number) :: text:
    loop split "%{_n}%" at "":
        add 1 to {_}
        add "%loop-value%" to {_d::*}
        if {_} >= 3:
            set {_} to 0
            add "," to {_d::*}
    set {_s} to size of {_d::*}
    loop integers between 0 and 1:
        set {_} to size of {_d::*} - loop-num
        if {_d::%{_}%} is ",":
            delete {_d::%{_}%}
    return "%join {_d::*} by """"%"
And if you're ignoring that

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

code_language.skript:
function numberDecimation(t: text) :: text: #Don't know if this line counts
    loop split {_t} at "":
        add 1 to {_}
        add "%loop-value%" to {_d::*}
        if {_} >= 3:
            set {_} to 0
            add "," to {_d::*}
    return "%join {_d::*} by """"%" #Don't know if this line counts
 
Last edited by a moderator:
Hi,
This is my skript :emoji_stuck_out_tongue:

Code:
function N(i: text) :: text:
   set {_r} to ""
   set {_l} to {_i}' length
   loop {_l} times:
       set {_r} to "%subtext of {_i} from {_l}-loop-value+1 to {_l}-loop-value+1%%{_r}%"
       if loop-value mod 3 is equal to 0:
           if loop-value is not {_l}:
               set {_r} to ".%{_r}%"
   return {_r}
 
Status
Not open for further replies.