if (!isNaN(result) && result % 1 !== 0) {
const digitsValue = result.toString().split(".")[1];
if (digitsValue && digitsValue.length > 6) {
result = result.toFixed(6).toString();
}
}
- if (!isNaN(result) && result % 1 !== 0): 이 부분은 result가 숫자가 아니거나 소수점을 가지는 경우를 확인합니다. isNaN() 함수는 주어진 값이 숫자인지 여부를 확인하며, result % 1 !== 0은 result가 정수가 아닌 경우를 확인합니다.
- const digitsValue = result.toString().split(".")[1] : result를 문자열로 변환한 후 소수점을 기준으로 분할하여 소수 부분만 추출합니다.
- if (digitsValue && digitsValue.length > 6) { }: 이 부분은 추출된 소수 부분이 존재하고 그 길이가 6보다 큰지 확인합니다.
- result = result.toFixed(6).toString(): 위 조건들이 모두 참일 경우, result를 소수점 이하 6자리까지 반올림하여 문자열로 변환합니다.
if (result.toString().includes(".")) {
result = result.toString();
} else if (!isNaN(result)) {
result = result.toLocaleString("en-Us", {
minimumFractionDigits: 0,
maximumFractionDigits: 3,
});
} else {
result = "";
}
- if (result.toString().includes(".")): 이 부분은 result를 문자열로 변환한 후, 해당 문자열에 소수점이 포함되어 있는지를 확인합니다. 즉, result가 소수인지를 검사합니다.
- result = result.toString();: 만약 result가 소수인 경우, 아무런 조작 없이 그대로 문자열로 변환하여 result에 할당합니다. 즉, 소수인 경우 변환 과정을 거치지 않고 그대로 유지됩니다.
- else if (!isNaN(result)): 만약 result가 숫자이면서도 소수가 아닌 경우를 검사합니다. 즉, 정수인 경우를 다룹니다.
- result = result.toLocaleString("en-Us", { minimumFractionDigits: 0, maximumFractionDigits: 6 });: result가 정수인 경우, toLocaleString() 메소드를 사용하여 해당 숫자를 미국 영어 기준으로 형식화합니다. minimumFractionDigits는 소수 부분의 최소 자릿수를 설정하고, maximumFractionDigits는 소수 부분의 최대 자릿수를 설정합니다. 이렇게 함으로써 정수가 소수로 변환되며, 소수 부분이 최대 6자리까지 나타날 것입니다.
- else: 위의 두 조건에 해당하지 않는 경우, 즉 result가 숫자도 아니고 소수도 아닌 경우를 다룹니다.
- result = "";: result를 빈 문자열로 설정하여 처리합니다. 이는 result가 숫자나 소수가 아닌 경우에 대해 빈 문자열로 처리하는 것입니다.