PageSourceSearch

https://maritimeheritageeast.org.uk/dlib/js/numerical.js?_t=1727039366

js maritimeheritageeast.org.uk collected 2026-10-02 14:17:03 UTC 5,493 bytes, 160 lines download raw bytes

1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//	DLib Numerical JavaScript functions - copyright davidviner.com 2009-2024 except where stated.
3//
4//	22.09.2024	7.3.0	DJV		Code reduced.
5//
6////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8'use strict';
9
10//C/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
11// Numerical functions.
12// @DLibNumerical
13////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
14
15var	DLibNumerical = function ()
16{
17	var _public =
18	{
19		////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20		// Used by formfields.php numberInput function.
21		////////////////////////////////////////////////////////////////////////////////////////////////////////////////
22
23		numberInput : function (fld, dp, md)
24		{
25			var fs = fld.style;
26
27			fs.textAlign = (md == 0 ? 'center' : 'right');
28
29			if (md == 1 && dp > 0)
30				fld.value = dec (fld.value, dp);
31		},
32
33		//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////
34		// Format a file size value (converts to Kb, Mb, Gb etc depending upon size specified in $sz).
35		// @formatFileSize	string	Formatted size.
36		// @sz		i	Size in bytes.
37		// @[dp]	i	Number of decimal places to show (default = 0, maximum = 3).
38		////////////////////////////////////////////////////////////////////////////////////////////////////////////////
39
40		formatFileSize : function (sz, dp)
41		{
42			var suffixes = [" bytes", "Kb", "Mb", "Gb", "Tb", "Eb"],
43				sz1 = 0,
44				n = 0,
45				suff = suffixes[n];
46
47			dp = setUD (dp, 0);
48
49			if (dp > 3)
50				dp = 3;
51
52			while (sz > 1024)
53			{
54				n++;
55				sz1 = sz / 1024;
56				sz = Math.round (sz1);
57				suff = suffixes[n];
58			}
59
60			if (dp > 0)
61			{
62				var sz2 = "" + sz1,
63					p = sz2.indexOf (".");
64
65				if (p > -1)
66					sz = sz2.substr (0, p + dp + 1);
67			}
68
69			return sz + suff;
70		},
71
72		//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////
73		// Return the number of decimal places of the supplied number.
74		// @decPlaces	i	Number of decimal places.
75		// @num			f	The number.
76		////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77
78		decPlaces : function (num)
79		{
80			var n = num - Math.floor (num),
81				ns = n.toString ();
82
83			return (ns.length > 2 ? ns.length - 2 : 0);
84		},
85
86		//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////
87		// Convert the supplied text into a string of hex.
88		// @toHex	s	The 'hexed' string.
89		// @str		s	The string to be converted.
90		// @[gap]	b	Add a space between each hex pair.
91		////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92
93		toHex : function (str, gap)
94		{
95			var s = "";
96			gap = setUD (gap, false);
97
98			for (var i = 0; i < str.length; i++)
99				s += '' + str.charCodeAt (i).toString (16) + (gap ? " " : "");
100
101			return s;
102		},
103
104		//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////
105		// Convert the 'hexed' string back into normal text.
106		// @deHex	s	The original string.
107		// @str		s	The 'hexed' string to be converted.
108		////////////////////////////////////////////////////////////////////////////////////////////////////////////////
109
110		deHex : function (str)
111		{
112			var	s = "";
113			str = str.replace (/ /g, "");
114
115			for (var i = 0; i < str.length; i += 2)
116				s += String.fromCharCode (parseInt (str.substr (i, 2), 16));
117
118			return s;
119		}
120	};
121
122	return _public;
123} ();
124
125//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
126// Round to the specified number of decimal places.
127// @dec	f	The formatted value.
128// @num	f	The number to format.
129// @dp	i	The number of decimal places.
130////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
131
132function dec (num, dp)
133{
134	var p = Math.pow (10, dp),
135		n = Math.round (num * p) / p;
136
137	return n.toFixed (dp);
138}
139
140//F/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
141// Round to specified dec places but add thousands separators. Based on code found at:
142// http://snipplr.com/view/3516/mootools--numberformat/
143// @tDec	s	The formatted number.
144// @num		f	The number to format.
145// @dp		i	The number of decimal places.
146////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
147
148function tDec (num, dp)
149{
150	// Returns matches[1] as sign, matches[2] as numbers and matches[3] as decimals
151
152	var matches = /(-)?(\d+)(\.\d+)?/.exec ((isNaN (num) ? 0 : num) + ''),
153		remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;
154
155	return (matches[1] ? matches[1] : '') + (remainder ? matches[2].substr (0, remainder) + ',' : '') +
156		matches[2].substr (remainder).replace (/(\d{3})(?=\d)/g, "$1" + ',') +
157		(dp ? '.' + (+ matches[3] || 0).toFixed (dp).substr (2) : '');
158}
159
160////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.