-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadder.html
80 lines (72 loc) · 3.57 KB
/
adder.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>number adder</title>
<meta property="og:site_name" content="orbit-loona.github.io" />
<meta property="og:type" content="website" />
<meta property="og:title" content="number adder" />
<meta property="og:url" content="https://orbit-loona.github.io/adder.html" />
<meta property="og:description" content="Quickly add a bunch of numbers" />
<meta property="og:locale" content="en_US" />
<meta property="og:image" content="https://orbit-loona.github.io/bin/addition.png" />
<meta property="og:image:width" content="197" />
<meta property="og:image:height" content="312" />
<meta property="og:image:alt" content="image" />
<meta name="twitter:image:src" content="https://orbit-loona.github.io/bin/addition.png" />
<meta name="twitter:title" content="number adder" />
<meta name="twitter:description" content="Quickly add a bunch of numbers" />
<link rel="stylesheet" href="main.css"/>
<meta name="SKYPE_TOOLBAR" content="SKYPE_TOOLBAR_PARSER_COMPATIBLE">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="An Orbit">
<meta charset="UTF-8">
<script>
const decimalPointUsingNumberRegex = /\-?[\d\,]+(\.\d+|)(e[+-]?[\d\,]+(\.\d+|)|)/g;
const decimalCommaUsingNumberRegex = /\-?[\d\,]+(\.\d+|)(e[+-]?[\d\,]+(\.\d+|)|)/g;
var useDecimalComma = false;
var decimalCharacter = useDecimalComma ? "," : ".";
var separatorCharacter = useDecimalComma ? "." : ",";
function parseNumber(numberText) {
numberText = numberText.replaceAll(separatorCharacter,"");
numberText = parseFloat(numberText);
return numberText
};
function getNumbers() {
var numberBox = document.getElementById("numberContainingText");
var numberList = numberBox.value;
numberList = numberList.match(useDecimalComma ? decimalCommaUsingNumberRegex : decimalPointUsingNumberRegex) ?? [];
numberList = numberList.map(function(number) {
if(number.includes("e")) {
number = number.split("e").map(x => parseNumber(x || "1"));
//console.log(number);
number = number[0] * (10 ** number[1]);
//console.log(number);
} else {
number = parseNumber(number);
};
return number
});
return numberList;
};
function showSum() {
var numberBox = document.getElementById("sumField");
var numbers = getNumbers();
//console.log(numbers);
var sum = numbers.reduce((a,b) => a + b,0);
numberBox.innerText = sum.toLocaleString(undefined,{maximumFractionDigits: 100})
return true
};
</script>
</head>
<body>
<h1>Quick and easy number summation</h1>
<p id="description">This page will extract numbers from the text field below and add them together. It does not support numbers in word form (e.g. "three", "six point five", or "one million two hundred sixty seven thousand five hundred twenty-three"), only numbers as digits (e.g. "3", "6.5", "1,267,523").<br/>
This tool <em>is</em> subject to JavaScript's <a href="https://0.30000000000000004.com/">floating-point number limitations</a>. Numbers may display slight inaccuracies due to binary rounding error, especially decimals.</p><br/>
<textarea id="numberContainingText" placeholder="Enter your text with numbers"></textarea><br/><br/>
<button onclick="showSum()">Add the contained numbers together</button>
<p>
Sum: <span id="sumField">[No addition has been performed yet]</span>
</p><br/>
<br/><br/><a href="index.html"><small>go to main page</small></a>
</body>
</html>