시간과 분 형식의 부동 소수점 수를 합산하는 방법은 무엇입니까?
그. 그러한 두 숫자 0.35+3.45
를 계산하면 = 4.20
이 아니 어야합니다.3.80
var arr = [0.15, 0.2, 3.45, 0.4, 2, 0.3, 5.2, 1, 1.4, 1.1, 2.4, 1, 3.4]
var sum = 0.0;
arr.forEach(function(item) {
console.log(sum);
sum += parseFloat(item);
});
결과는 다음과 같아야합니다.
0
0.15
0.35
4.20
5.0
7.0
7.30
12.50
13.50
15.30
16.40
19.20
20.20
답변
이와 같은 “미친”데이터 형식을 처리하는 가장 좋은 방법은 먼저 데이터 형식을 적절하고 쉽게 처리 할 수있는 형식으로 변환하고 변환 된 데이터를 사용하여 수행해야하는 모든 작업을 수행 한 다음 최종적으로 결과를 다시 변환하는 것입니다. 원래 형식
(물론 실제 해결책은 그러한 미친 시간 표현의 사용을 완전히 중단하는 것입니다. 그러나 항상 실용적이지는 않습니다. 예를 들어 변경할 수없는 레거시 시스템과 인터페이스해야하기 때문입니다.)
귀하의 경우 데이터에 적합한 적절한 형식은 예를 들어 정수입니다. 데이터를이 형식으로 변환 한 후에는 일반적인 산술을 수행 할 수 있습니다.
// converts a pseudo-float of the form hh.mm into an integer number of minutes
// robust against floating-point roundoff errors, also works for negative numbers
function hhMmToMinutes(x) {
const hhmm = Math.round(100 * x) // convert hh.mm -> hhmm
return 60 * Math.trunc(hhmm / 100) + (hhmm % 100)
}
// convert minutes back to hh.mm pseudo-float format
// use minutesToHhMm(minutes).toFixed(2) if you want trailing zeros
function minutesToHhMm(minutes) {
return Math.trunc(minutes / 60) + (minutes % 60) / 100
}
const arr = [0.15, 0.2, 3.45, 0.4, 2, 0.3, 5.2, 1, 1.4, 1.1, 2.4, 1, 3.4]
let sum = 0
console.log( arr.map(hhMmToMinutes).map(x => sum += x).map(minutesToHhMm) )
위의 변환 코드는 먼저 입력 float에 100을 곱한 후 시간과 분을 분리하기 전에 정수로 반올림합니다. 이것은 입력을 스트링화할 필요없이 반올림 오류가 발생할 수있는 입력을 강력하게 처리해야합니다.
여기에서 특별한주의를 기울여야하는 이유는 숫자 0.01 = 1/100 (및 대부분의 배수)이 실제로 JavaScript에서 사용되는 이진 부동 소수점 형식으로 정확하게 표현할 수 없기 때문 입니다. 따라서 당신이 실제로 일 것 JS 번호 가질 수 없습니다 정확히 0.01 같음 – 당신이 할 수있는 최선은 너무 가까이 문자열로의 변환은 자동으로 오류를 숨길 것입니다 숫자입니다. 그러나 반올림 오류는 여전히 존재하며 이러한 숫자를 정확한 임계 값과 비교하는 것과 같은 일을 시도하면 물릴 수 있습니다. 다음은 훌륭하고 간단한 데모입니다.
console.log(Math.trunc(100 * 0.29)) // you'd think this would be 29...
답변
// We got this after simple addition
// Now we want to change it into 4.2
sample = 3.8
// Now here are the minutes that the float currently shows
minutes = (sample % 1) * 100
// And the hours
hours = Math.floor(sample)
// Here are the number of hours that can be reduced from minutes
AddHours = Math.floor(minutes / 60)
// Adding them to the hours count
hours += AddHours
// Reducing mintues
minutes %= 60
// Finally formatting hour and minutes into your format
final = hours + (minutes / 100.0)
console.log(final)
간단한 산술 덧셈을 한 후에이 논리를 사용할 수 있습니다. 그러면 합을 시간 형식으로 변환합니다
답변
Javascript ES6으로 구현했습니다. 각 요소를 반복하고 시간을 분 단위로 나누고 .
분이 존재하지 않는지 확인했습니다. 그렇지 않으면 0에 총 시간과 분을 카운트하고 필요한 계산을 적용하십시오.
const resultObj = [0.35, 3.45].reduce((a, e) => {
const [hours, minutes] = e.toString().split('.');
a.h += +hours;
a.m += (+(minutes.padEnd(2, 0)) || 0);
return a;
}, {h: 0, m: 0});
const result = resultObj.h + Math.floor(resultObj.m / 60) + ((resultObj.m % 60) / 100);
console.log(result);
답변
모든 것을 몇 시간 또는 몇 분으로 변환 한 다음 수학을 수행해야합니다.
Example: 0.35 + 3.45
Convert 0.35 to Hours Number((35/60).toFixed(2))
= 0.58
Then convert 3hours 45 minutes into hours
= 3 + Number((45/60).toFixed(2))
= 3hours + 0.75 hours
= 3.75
Add the 2 conversions
= 0.58 + 3.75
= 4.33 hours
Convert the 0.33 back to minutes by multiplying by 60
= 4 hours 19.8 minutes ~ 20 mins
Hope that helps!
답변
먼저 시간과 분으로 변환해야합니다
var arr = [0.15, 0.2, 3.45, 0.4, 2, 0.3, 5.2, 1, 1.4, 1.1, 2.4, 1, 3.4];
function add(arr) {
var hm = arr
.map(v => [Math.floor(v), (v % 1).toFixed(2) * 100]) // Map the array to values having [hours, minutes]
.reduce((t, v) => { // reduce the array of [h, m] to a single object having total hours and minutes
t = {
h: t.h + v[0],
m: t.m + v[1]
};
return t;
}, {
h: 0,
m: 0
});
// final result would be =
// total hours from above
// + hours from total minutes (retrived using Math.floor(hm.m / 60))
// + minutes (after converting it to be a decimal part)
return (parseFloat(hm.h + Math.floor(hm.m / 60)) + parseFloat(((hm.m % 60) / 100).toFixed(2))).toFixed(2);
}
// adding `arr` array
console.log(add(arr));
// adding 0.35+3.45
console.log(add([0.35, 3.45]));
답변
첫 번째 결과가 0 이되는 이유를 알 수 있습니까? 아래는 단일 루프로 이것을 수행하는 방법입니다. 주석은 코드에 설명되어 있습니다. 각 루프마다 분을 추가하고 60보다 큰 경우 1 시간을 더한 다음 % 60 을 사용 하여 남은 분을 얻습니다. 플로트는 작업하기가 어려울 수 있습니다.
예 : 1.1 + 2.2 === 3.3은 거짓
그래서 숫자를 문자열로 변환 한 다음 int로 다시 변환했습니다.
var arr = [0.15, 0.2, 3.45, 0.4, 2, 0.3, 5.2, 1, 1.4, 1.1, 2.4, 1, 3.4];
let hoursTally = 0;
let minutesTally = 0;
for(var i = 0; i < arr.length; i++) {
// Convert the float to a string, as floats can be a pain to add together
let time = arr[i].toString().split('.');
// Tally up the hours.
hoursTally += parseInt(time[0]);
// Tally up the minutes;
let minute = time[1];
if(minute) {
// We do this so we add 20 minutes, not 2 minutes
if(minute.length < 2) {
minutesTally += parseInt(minute) * 10;
} else {
minutesTally += parseInt(minute);
}
// If the minutesTally is greater than 60, add an hour to hours
if(minutesTally >= 60) {
hoursTally++;
// Set the minutesTally to be the remainder after dividing by 60
minutesTally = minutesTally % 60;
}
}
console.log(hoursTally + "." + minutesTally);
}
답변
const arr = [0.15, 0.2, 3.45, 0.4, 2, 0.3, 5.2, 1, 1.4, 1.1, 2.4, 1, 3.4]
const reducer = (acc, curr) => {
acc = `${acc}`.split('.').length > 1 ? acc : `${acc}.0`;
curr = `${curr}`.split('.').length > 1 ? curr : `${curr}.0`;
let hours = parseStrAndSum(true, `${acc}`.split('.')[0], `${curr}`.split('.')[0]);
let minute = parseStrAndSum(false, `${acc}`.split('.')[1], `${curr}`.split('.')[1]);
hours = parseInt(hours / 1) + parseInt(minute / 60);
minute = minute % 60;
console.log(`${acc} + ${curr} = ${hours}.${minute}`);
return `${hours}.${minute}`;
}
const parseStrAndSum = (is_hours, ...strs) => {
let result = 0;
strs.forEach(function(number) {
if (!is_hours) {
number = number.length == 1 ? `${number}0` : number;
}
result += parseInt(number);
});
return result;
};
arr.reduce(reducer);
