Created
March 27, 2023 20:03
-
-
Save orbit-loona/052674efc0bec2436d35e4af12ebad6d to your computer and use it in GitHub Desktop.
Tusidan number converter (digits)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Why does this exist | |
/*Floating point warning: This is subject to the limitations of the IEEE 754 | |
double-precision binary floating-point type JavaScript uses. This can lead | |
to precision loss. If you are converting big numbers with lots of decimals | |
(like the number 1,000,000,000,000.39930209809), this loss can be severe.*/ | |
//from https://stackoverflow.com/questions/6259515/how-can-i-split-a-string-into-segments-of-n-characters | |
//modified and more modified | |
function splitString(str,chars) { | |
var chunks = []; | |
for (var i = 0, charsLength = str.length; i < charsLength; i += chars) { | |
chunks.push(str.substring(i, i + chars)); | |
}; | |
return chunks; | |
}; | |
function splitStringFromEnd(str,chars) { | |
var chunks = []; | |
for (var i = str.length, charsLength = 0; i > -1; i -= chars) { | |
chunks.unshift(str.substring(i - chars, i)); | |
}; | |
return chunks.filter(function(str) { return str !== '' }); | |
}; | |
function numberToTusidan(number,doSeparators=true) { | |
if(!(["bigint","number"].includes(typeof(number)))) { | |
throw new TypeError("Input must be a number or bigint"); | |
}; | |
var numberBase12 = number.toString(12); | |
var result = ""; | |
digits = "0123456789ab."; | |
var tusidanDigits = "८r^ⵠп□ⅎㅂ⧄≡7૯`"; | |
for(i = 0; i < numberBase12.length; i++) { | |
ind = digits.indexOf(numberBase12.charAt(i)); | |
result += tusidanDigits[ind] ?? numberBase12.charAt(i); | |
}; | |
if(doSeparators) { | |
result = result.split("`"); | |
result[0] = splitStringFromEnd(result[0],4).join("."); | |
if(result[1]) { | |
result[1] = splitString(result[1],4).join("."); | |
}; | |
result = result.join("`"); | |
}; | |
return result; | |
}; | |
//https://stackoverflow.com/questions/8555649/second-argument-to-parsefloat-in-javascript | |
function parseFloatWithRadix(s, r) { | |
r = (r||10)|0; | |
const [b,a] = ((s||'0') + '.').split('.'); | |
const l1 = parseInt('1'+(a||''), r).toString(r).length; | |
return parseInt(b, r) + | |
parseInt(a||'0', r) / parseInt('1' + Array(l1).join('0'), r); | |
} | |
function numberFromTusidan(numberString) { | |
var tusidanAlphabet = ["८", "r", "^", "ⵠ", "п", "□", "ⅎ", "ㅂ", "⧄", "≡", "7", "૯", "`", "."]; | |
haBaseTwelveDigits = ["0","1","2","3","4","5","6","7","8","9","a","b",".",""]; | |
var result = ""; | |
for(i = 0; i < numberString.length; i++) { | |
ind = tusidanAlphabet.indexOf(numberString.charAt(i)); | |
result += haBaseTwelveDigits[ind] ?? numberString.charAt(i); | |
}; | |
var numberValue = parseFloatWithRadix(result,12); | |
if(isNaN(numberValue)) { | |
throw new Error(`NaN value from converting number ${numberString}`); | |
}; | |
return numberValue; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment