12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- export const handleKeyDown = (e) => {
- if(e.keyCode === 13){
- document.getElementById("input_carry").focus();
- }
- };
- export const afterCommaLen = (number) => {
- let noStrList = number.toString().split(".");
- if (noStrList.length > 1){
- return noStrList[1].length;
- }
- return 0;
- };
- export function numbersToArrOfArr(numbers) {
- let befComma = Math.max(...numbers).toString().split(".")[0].length;
- let afterComma = Math.max(...numbers.map(x => afterCommaLen(x)));
- let withComma = Math.max(...numbers.map(x => x.toString().indexOf(".")));
- let arrLength;
- if(withComma < 0){ // no comma found
- arrLength = befComma;
- }else{
- arrLength = befComma + afterComma + 1; // add comma and after comma len
- }
- let numbersArr = numbers.map(x => x.toString().split(""));
- for (let numArr of numbersArr){
- let commaIdx = numArr.indexOf(".");
- // without comma, add before
- while(commaIdx===-1 && numArr.length < befComma){
- numArr.unshift(" "); // add " " before comma
- }
- // without comma, add after
- while(commaIdx===-1 && numArr.length < arrLength){
- numArr.push(" "); // add " " after comma
- }
- // with comma, add before
- while(commaIdx>-1 && commaIdx!==befComma){
- numArr.unshift(" "); // add " " before comma
- commaIdx = numArr.indexOf(".");
- }
- // with comma, add after
- while(numArr.length < arrLength){
- numArr.push(" "); // add " " after comma
- }
- numArr.unshift(" "); // for carry
- }
- return [numbersArr, befComma+1];
- // TODO: assert all have same length and comma at same index?
- }
|