Last active
October 31, 2015 11:30
-
-
Save kob-to-wni/16972caf837ece8cae07 to your computer and use it in GitHub Desktop.
XDecimal
This file contains hidden or 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
| var xd = new XDecimal("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); | |
| console.log(xd.toString(10)); | |
| console.log(xd.parse("a")); | |
| console.log(xd.toString(1445598572.04283)); | |
| console.log(xd.parse("1zPAe0.175")); |
This file contains hidden or 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
| function XDecimal(seed){ | |
| var SEED = seed.split(""); | |
| var SEED_LENGTH = SEED.length; | |
| function toString(value) { | |
| if(value % 1 != 0){ | |
| var p = value.toString().split("."); | |
| return toString(p[0]) + "." + toString(p[1]); | |
| } | |
| var v = value; | |
| var c = []; | |
| while (0 < v) { | |
| var m = v % SEED_LENGTH; | |
| c.push(SEED[m]); | |
| v = (v - m) / SEED_LENGTH; | |
| } | |
| return c.reverse().join(""); | |
| } | |
| function parse(value) { | |
| if(0 < value.indexOf(".")){ | |
| var p = value.split("."); | |
| return parseFloat(parse(p[0]) + "." + parse(p[1])); | |
| } | |
| var v = 0; | |
| var c = value.split(""); | |
| for(var i = 0; i < c.length; i++) { | |
| var m = SEED.indexOf(c[i]); | |
| if(m < 0){ | |
| throw new Error(); | |
| } | |
| v *= SEED_LENGTH; | |
| v += m; | |
| } | |
| return v; | |
| } | |
| this.toString = toString; | |
| this.parse = parse; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment