javascript / K345 / number conversion & validation
Description
Lorem ipsum.
- K345.numString2Number()
- parse formatted numeral string
- K345.numericInput()
- convert strings of many numeral systems
- K345.rom2dec() / K345.dec2rom()
- convert from/to roman. Supports numbers up to 1,000,000,000
Requirements
- K345 basic methods
- for older browsers: Array methods polyfill
Download
- download methods (minified)
- download methods
- view methods source
- download array methods polyfill (minified)contains:
• Array.isArray
• Array.prototype.every
• Array.prototype.filter
• Array.prototype.forEach
• Array.prototype.indexOf
• Array.prototype.lastIndexOf
• Array.prototype.map
• Array.prototype.reduce
• Array.prototype.reduceRight
• Array.prototype.some
K345.numString2Number()
Convert a (formatted) string into a numeric value. Default output is decimal (radix=10); if a radix parameter is provided and radix is an integer 2 ≤ radix ≤ 36, the output value will be converted to that numeral system.
Spaces, dots and commas may be used as grouping chars and / or as decimal point (see rules table).
Usage
Number|NaN num = K345.numString2Number(String numString [, Integer radix=10]);
Conversion rules
Lorem ipsum ...
String contains | Chars will be treated as |
---|---|
spaces | grouping chars |
two ore more dots; no commas | |
two ore more commas; no dots | |
exactly one comma | decimal point |
exactly one dot | |
mixed commas & dots | last dot / comma as decimal point any other as grouping chars |
Examples
usage examples
dec = K345.numString2Number('10000001'); // 10000001 dec = K345.numString2Number('10 000 001'); // 10000001 dec = K345.numString2Number('10,000,001'); // 10000001 dec = K345.numString2Number('10.000.001'); // 10000001 dec = K345.numString2Number('10000.001'); // 10000.001 dec = K345.numString2Number('10000,001'); // 10000.001 dec = K345.numString2Number('10 000.001'); // 10000.001 dec = K345.numString2Number('10,000.001'); // 10000.001 dec = K345.numString2Number('10.000,001'); // 10000.001
K345.numericInput()
Converts a string containing a numeral representation to a decimal value, if the string is valid.
This method can be used to process user input, e.g. from an <input> element. If the user input is not valid by the rules of chosen numeral system or if the user input is valid, but the chosen numeral system is disabled, this method returns NaN. With the second parameter types of accepted numeral strings can be enabled/disabled. Behavior can be tested in a test area below.
Input types can be:
- decimal
- binary
- octal
- hexadecimal
- scientific E notation (calculator)
- exponential & root
- roman numbers
- boolean
- any numeral system from base 2 to 36
Usage
Number|NaN decimal = K345.numericInput(String numString [, Object enabledNumSystems]);
String syntax
K345.numericInput() recognizes the numeral system according to certain syntax rules as described below. It is possible to group chars of the numeral string with spaces, e.g. "0b 1011 0101 1101" or "31 415 926 535 E -10" or "MM CM XL IV".
numeral system | syntax rules |
---|---|
Decimal | string composed of 0-9, optionally prefixed by 0d. |
Binary | string composed of 0 and 1, prefixed by 0b. |
Octal | string composed of 0-7, prefixed by 0o. |
Hexadecimal | string composed of 0-9 and A-F, prefixed by 0x or #. |
Scientific E notation | string composed of value followed by E followed by exponent of base 10. |
Exponential notation | string composed of value followed by ^ or pow or ** followed by exponent. |
Root notation | string composed of root degree (optional, defaults to 2 → square root), followed by √ or root or rt, followed by value. |
Roman | see K345.parseRoman. |
Other systems | string composed of valid chars for selected numeral system; followed by sytem base in the range of 2 to 36, surrounded by square brackets. |
Boolean | boolean true or false; strings "true" or "false" |
color index: placeholder for value; literal char(s) or words; definition or default |
char values for numeral systems:
A=10, B=11, C=12, D=13, E=14, F=15, G=16, H=17, I=18, J=19, K=20, L=21, M=22, N=23, O=24, P=25, Q=26, R=27, S=28, T=29, U=30, V=31, W=32, X=33, Y=34, Z=35,
Valid char values for each numeral system are 0 to (base - 1).
Examples: If base is 5, only 0-4 are valid chars. If base is 16 (hex), only 0-15 (chars 0-9 plus letters A-F) are valid. If base is 22, only 0-21 (chars 0-9 plus letters A-L) are valid.
Examples
Show usage examples
o = {all: true}; // enable all numeral system conversions // hexadecimal dec = K345.numericInput('0xFF', o); // 255 dec = K345.numericInput('-0xA0', o); // -160 dec = K345.numericInput('#FF', o); // 255 // binary dec = K345.numericInput('0b1000', o); // 8 dec = K345.numericInput('-0b1110', o); // -14 // decimal dec = K345.numericInput('0d345', o); // 345 dec = K345.numericInput('22', o); // 22 dec = K345.numericInput('007', o); // 7 (leading 0 doesn't trigger octal) // octal dec = K345.numericInput('0o15', o); // 13 dec = K345.numericInput('0o100', o); // 64 // e notation dec = K345.numericInput('2E3', o); // 2000 dec = K345.numericInput('-3.14E3', o); // -3140 dec = K345.numericInput('2222E-2', o); // 22.22 // exponential and root dec = K345.numericInput('2 ^ 3', o); // 8 dec = K345.numericInput('4 pow 3', o); // 64 dec = K345.numericInput('5 ** 4', o); // 625 dec = K345.numericInput('3 √ 27', o); // 3 dec = K345.numericInput('√ 16', o); // 4 dec = K345.numericInput('root 25', o); // 5 dec = K345.numericInput('4 rt 16', o); // 2 // Boolean conversion dec = K345.numericInput('true', o); // 1 dec = K345.numericInput(true, o); // 1 dec = K345.numericInput('false', o); // 0 dec = K345.numericInput(false, o); // 0 // misc numeral systems dec = K345.numericInput('FF[16]', o); // 255 (hey, that's hex!) dec = K345.numericInput('-42[10]', o); // -42 (base 10 => decimal) dec = K345.numericInput('hi[23]', o); // 409 (17*23 + 18) dec = K345.numericInput('200[12]', o); // 288 (2*12*12 + 0 * 12 + 0 * 1) // roman dec = K345.numericInput('MMX', o); // 2010 dec = K345.numericInput('V*M D CC L I', o); // 5751 dec = K345.numericInput('M X*M', o); // 9000 dec = K345.numericInput('C*M M*M M', o); // 901000 dec = K345.numericInput('IV', o); // 4 dec = K345.numericInput('IIII', o); // 4
Enable / Disable numeral systems
It is possible to allow only specific numeral systems by setting the value(s) of corresponding properties of an object to true or false. This object has to be provided as second parameter to K345.numericInput. For each property not in this object, the default value will be taken.
By default, the most common numeral systems are enabled:
method accepts | property | enabled |
---|---|---|
decimal | dec | true |
hexadecimal | hex | true |
binary | bin | true |
octal | oct | true |
scientific E notation | sci | true |
exponential and root | exp | false |
convert boolean | boo | false |
roman numbers | rom | false |
other numeral systems | num | false |
To enable all numeral systems, set the second parameter to {all: true}
. All additional properties will be ignored. It is not possible to disable all numeral systems with {all: false}
.
examples
// examples for second parameter. {rom: true} // enabled: all default systems plus roman numbers {dec: false, oct: false, num: true, sci: false} // enabled: hexadecimal, binary, other numeral systems {rom: true, num: true, boo: true, exp: true} {all: true} // enabled: all systems {all: false} // "all" property will be ignored if not true
Test area for K345.numericInput()
javascript is required for this feature.
test this method
K345.rom2dec() / K345.dec2rom()
Lorem ipsum
Usage
Number|NaN num = K345.rom2dec(String roman);
String roman = K345.dec2rom(Number num [,Boolean group=false]);
Formatting and special chars
K345.rom2dec() allows (but does not require) grouping chars to increase readability. Allowed chars are " " (space), "(" and ")"
Some alternate special chars are allowed for certain values/operations:
char | same as | value/op | code |
---|---|---|---|
• | * | multiply | \u2022 |
Ↄ | D | 500 | \u2183 |
ↀ | M | 1000 | \u2180 |
ↁ | V•M | 5000 | \u2181 |
ↂ | X•M | 10000 | \u2182 |
Examples
usage examples
dec = K345.rom2dec('MCMLXXXIV'); // 1984 dec = K345.rom2dec('VIIII'); // 9 dec = K345.rom2dec('XC'); // 90 dec = K345.rom2dec('LXXXX'); // 90 dec = K345.rom2dec('XLII'); // 42 dec = K345.rom2dec('V*M'); // 5000 dec = K345.rom2dec('V•M'); // 5000 dec = K345.rom2dec('M X*M'); // 9000 => 1000 10*1000 => (10*1000)-1000 dec = K345.rom2dec('X*M'); // 10000 dec = K345.rom2dec('L*M'); // 50000 dec = K345.rom2dec('C*M'); // 100000 dec = K345.rom2dec('D*M'); // 500000 dec = K345.rom2dec('M*M'); // 1000000 dec = K345.rom2dec('(X*M)(C*M)'); // 90000 => 10*1000 100*1000 => (100*1000)-(10*1000) dec = K345.rom2dec('L•M X•M X•M X•M X•M'); // 90000 dec = K345.rom2dec('C•M M•M X•M C•M M X•M CM XC IX'); // 999999 str = K345.dec2rom(90000); // X•MC•M str = K345.dec2rom(2019); // MMXIX str = K345.dec2rom(4444); // MV•MCDXLIV str = K345.dec2rom(4444, true); // MV•M CD XL IV str = K345.dec2rom(8948, true); // V•M M M M CM XL V I I I