RSS
 

Posts Tagged ‘decimal’

Convert Base 10 to Base 64 and vise versa using JavaScript

13 Jan

This is the way to convert base 10 (a decimal number) into a base 64 string and base 64 into a base 10, while the order of that base is as following:

0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-

Base 10 to Base 64

1
2
3
4
5
6
7
8
9
10
11
12
function base10_to_base64(num) {
    var order = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";
    var base = order.length;
    var str = "", r;
    while (num) {
        r = num % base
        num -= r;
        num /= base;
        str = order.charAt(r) + str;
    }
    return str;
}

Read the rest of this entry »

 

Test if the given value is a decimal number or not in JavaScript using Regular Expression

26 May

To identify in JavaScript whether the number or a string is in a decimal format or not, is actually pretty easy and short.
It looks obvious, but actually it doesn’t due to several different combinations of decimal number representation in JavaScript.

I’m using Regular Expression to test it:

1
2
3
function isDecimal(number) {
    return /^\d+(\.\d+|e)(e?-\d+)?$/.test(number);
}

Read the rest of this entry »