-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_num_to_str.ks
46 lines (43 loc) · 1.07 KB
/
lib_num_to_str.ks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// This file is distributed under the terms of the MIT license, (c) the KSLib team
@LAZYGLOBAL off.
function num_to_str {
parameter
number, //input number
ip, //number of digits before the decimal point.
dp. //number of decimal places
local string is "".
local padder is "".
local absNumber is abs(number).
local index is ip-1.
local firstNum is false.
until firstNum or index = 0 { // stop adding spacers when the first number is found
if mod(floor(absNumber/10^index),10) = 0 {
set padder to padder +" ".
}
else {
set firstNum to true.
}
set index to index-1.
}.
if dp = 0 {
set string to string +round(absNumber).
}.
else {
// set index to index-1.
set string to string +floor(absNumber).
set index to -1.
set string to string +".".
until index = -dp {
set string to string +mod(floor(absNumber/10^index),10).
set index to index-1.
}.
set string to string + mod(round(absNumber/10^index),10).
}.
if number < 0 {
set string to padder +"-" +string.
}
else {
set string to padder +" " +string.
}.
return string.
}.