Arithmetic & Decimals

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

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Status
Not open for further replies.

xGrow

Member
Feb 26, 2017
10
1
0
27
So, i'm coding a system of KDR:
CODE:
code_language.skript:
on every second:
    set {kdr.%player%} to {kills.%player%}/{deaths.%player%}
And, if the result give to player a giant number i want reducing to 2 decimals like: "0,435687548" to "0,43"
 
Last edited:
Skript has an option in the config.sk to select how much decimal digits will be displayed on a number, it's the number accuracy.
 
If you cannot use "number accuracy" in config.sk for some reason, try rounding down multiplied number.
code_language.skript:
on every second:
    set {kdr.%player%} to (rounded down ({kills.%player%}*100)/{deaths.%player%})/100
 

that would give an integer without decimals, and why are you multiplying it by 100 then dividing it by 100, I don't see the logic on that.

Also, I wouldn't use the periodical event for this kind of things, or even set a variable for it such as it's useless when all that you have to do is divide the kills by the deaths, setting a local variable where you need it would be better, other thing, you should use list variables instead of the regular ones for this kind of stuff. For example, a day you want to do a top kills & top deaths, then on this case, if you use variables like:
code_language.skript:
{kills::%player%} & {deaths::%player%}
instead of:
code_language.skript:
{kills.%player%} & {deaths.%player%}
will do it a lot easier, such as you'll be able to loop that list and get the value & index from that list, which is a thing that you will not be able to do with the regular variables.
 
The thing is, config.sk is global, and people want accuracy in some script and approximation in another one. An expression in Skript or in skUtilities would be good. Unless Java has some functions I don't know of, the solution is kinda archaic, because it involves substring/subtext. Can't detail it rn, but if anyone is down for using subtext/substring, name sure to post it here.
 
code_language.skript:
set {_kdr::*} to split "%{kills.%player%}/{deaths.%player%}%" by "."
set {_kdr} to "%{_kdr::1}%.%first 2 characters of {_kdr::2}%" parsed as num #You can remove the parsed as num if you want.
 
code_language.skript:
set {_kdr::*} to split "%{kills.%player%}/{deaths.%player%}%" by "."
set {_kdr} to "%{_kdr::1}%.%first 2 characters of {_kdr::2}%" parsed as num #You can remove the parsed as num if you want.
I think this code will cause parse error if the KDR was natunal number.

↓ test script
code_language.skript:
command /testkdr <number> <number>:
    trigger:
        set {_kdr} to (rounded down (arg-1*100)/arg-2)/100
        message "%{_kdr}%"


command /testkdr2 <number> <number>:
    trigger:
        set {_kdr::*} to split "%arg-1/arg-2%" by "."
        set {_kdr} to "%{_kdr::1}%.%first 2 characters of {_kdr::2}%" parsed as num #You can remove the parsed as num if you want.
        message "%{_kdr}%"
 
Status
Not open for further replies.