-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (80 loc) · 3.61 KB
/
Copy pathscript.js
File metadata and controls
96 lines (80 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
document.addEventListener('DOMContentLoaded', () => {
const moneyInput = document.getElementById('money-input');
const resDays = document.getElementById('res-days');
const resHours = document.getElementById('res-hours');
const resMinutes = document.getElementById('res-minutes');
const resSeconds = document.getElementById('res-seconds');
const resExact = document.getElementById('res-exact');
const CONSTANT_MBG = 1200000000000; // 1.2 Triliun Rp
// Format number to handle Indonesian locale formatting for thousands (dots)
function formatNumber(numStr) {
// Remove non-digit characters
let num = numStr.replace(/\D/g, '');
if (!num) return '';
// Format with thousand separator (default ID locale uses dots)
return parseInt(num, 10).toLocaleString('id-ID');
}
// Parse formatted string back to a numeric float
function parseFormattedNumber(str) {
let num = str.replace(/\D/g, '');
return num ? parseFloat(num) : 0;
}
// Format the result to properly handle many decimals and avoid scientific notation
function formatResult(num, maxDecimals = 10) {
if (num === 0) return '0';
// Use ID locale to format decimals with comma correctly
let str = num.toLocaleString('id-ID', {
minimumFractionDigits: 0,
maximumFractionDigits: maxDecimals
});
return str;
}
// Input event listener to calculate real-time
moneyInput.addEventListener('input', function (e) {
// Handle cursor position to keep it in correct place after reformatting
let cursorPosition = this.selectionStart;
let originalLength = this.value.length;
// Strip non-digits and reformat
let formattedStr = formatNumber(this.value);
this.value = formattedStr;
// Adjust cursor position based on adding/removing separators
let newLength = this.value.length;
cursorPosition = cursorPosition + (newLength - originalLength);
// Attempt to set cursor (works for input type="text")
if (this.setSelectionRange) {
this.setSelectionRange(cursorPosition, cursorPosition);
}
// Perform calculation
calculateMBG();
});
function calculateMBG() {
let money = parseFormattedNumber(moneyInput.value);
if (money === 0) {
resDays.textContent = '0';
resHours.textContent = '0';
resMinutes.textContent = '0';
resSeconds.textContent = '0';
if (resExact) resExact.textContent = '0 detik';
return;
}
// Calculation mapping
let days = money / CONSTANT_MBG;
let hours = days * 24;
let minutes = hours * 60;
let seconds = minutes * 60;
// Output formatting:
// Day needs the most decimals (e.g. up to 12)
// Others can have up to 6 decimals based on magnitude
resDays.textContent = formatResult(days, 12);
resHours.textContent = formatResult(hours, 8);
resMinutes.textContent = formatResult(minutes, 6);
resSeconds.textContent = formatResult(seconds, 4);
if (resExact) {
if (days >= 1) resExact.textContent = formatResult(days, 2) + " hari";
else if (hours >= 1) resExact.textContent = formatResult(hours, 2) + " jam";
else if (minutes >= 1) resExact.textContent = formatResult(minutes, 2) + " menit";
else if (seconds >= 0.01) resExact.textContent = formatResult(seconds, 2) + " detik";
else resExact.textContent = formatResult(seconds, 5) + " detik";
}
}
});