Skip to content

Radagaisus/round-to-human

Repository files navigation

Round to Human

Will return a nice rounded number that's easily digestible by humans. This is useful, for example, to programmatically generate an ever increasing number of bonus points, without generating numbers that looks "weird".

npm install round-to-human

https://nodei.co/npm/round-to-human.png?downloads=true

build status

# Will return a nice rounded number that's easily digestible by humans. This is
# useful, for example, to programmatically generate an ever increasing number of
# bonus points, without generating numbers that look "weird".
# 
# @param n - {Number} the number we want to round
# @param options - {Object}:
#   - threshold - Start rounding numbers only if they're above the threshold. By
#     default, 10.
#   - multiple - The multiple to use. Set to 10 by default. 5 is also recommended.
#   - upwards - whether to always round upwards or not.
#   - significant - number of significant digits to keep
# 
module.exports = round_to_human = (n, options = {}) ->
	# Set the threshold to 10 by default.
	options.threshold ||= 10
	# Set the multiple to 10 by default.
	options.multiple ||= 10
	# Set the significant digits to 2 by default
	options.significant ||= 2
	# Set the upward setting to true by default
	options.upwards ?= true
	# Round N to an integer
	n = Math.round(n)
	# Return the rounded number if it's lesser than or equal to the threshold
	return n if n <= options.threshold
	# Square 10 by the number of digits in `n` minus the significant digits
	exponent = n.toString().length - options.significant - 1
	len = Math.pow(10, exponent) * options.multiple
	# Get the maximum between the multiple and the length, in case the digits in
	# the number are lesser than or equal to the significant digits
	len = Math.max(options.multiple, len)
	# Round the number, plus the length, with the length as the multiple
	if options.upwards and n % len
	then round(n + len / 2 - 0.1, len)
	else round(n, len)
t = 1
for i in [0..200]
	t *= 1.15
	console.log "#{t}: #{round_to_human(t, multiple: 5)}"

1.15: 1
1.3224999999999998: 1
1.5208749999999995: 2
1.7490062499999994: 2
2.0113571874999994: 2
2.313060765624999: 2
2.6600198804687487: 3
3.0590228625390607: 3
3.5178762919199196: 4
4.0455577357079076: 4
4.652391396064093: 5
5.350250105473707: 5
6.152787621294762: 6
7.075705764488976: 7
8.137061629162321: 8
9.35762087353667: 9
10.761264004567169: 15
12.375453605252243: 15
14.231771646040078: 15
16.36653739294609: 20
18.821518001888002: 20
21.6447457021712: 25
24.891457557496878: 25
28.625176191121408: 30
32.918952619789614: 35
37.856795512758055: 40
43.53531483967176: 45
50.065612065622524: 50
57.5754538754659: 60
66.21177195678578: 70
76.14353775030364: 80
87.56506841284917: 90
100.69982867477654: 105
115.80480297599301: 120
133.17552342239196: 135
153.15185193575076: 155
176.12462972611337: 180
202.54332418503037: 205
232.9248228127849: 235
267.8635462347026: 270
308.043078169908: 310
354.24953989539415: 355
407.38697087970326: 410
468.4950165116587: 470
538.7692689884075: 540
619.5846593366686: 620
712.5223582371688: 715
819.400711972744: 820
942.3108187686556: 945
1083.6574415839539: 1100
1246.206057821547: 1250
1433.1369664947788: 1450
1648.1075114689954: 1650
1895.3236381893446: 1900
2179.622183917746: 2200
2506.565511505408: 2550
2882.5503382312186: 2900
3314.932888965901: 3350
3812.1728223107857: 3850
4383.998745657404: 4400
5041.598557506014: 5050
5797.8383411319155: 5800
6667.5140923017025: 6700
7667.641206146957: 7700
8817.787387069: 8850
10140.455495129348: 10500
11661.52381939875: 12000
13410.752392308561: 13500
15422.365251154844: 15500
17735.72003882807: 18000
20396.078044652277: 20500
23455.489751350116: 23500
26973.813214052632: 27000
31019.885196160525: 31500
35672.8679755846: 36000
41023.79817192229: 41500
47177.367897710625: 47500
54253.973082367214: 54500
62392.069044722295: 62500
71750.87940143063: 72000
82513.51131164523: 83000
94890.538008392: 95000
109124.1187096508: 110000
125492.7365160984: 130000
144316.64699351316: 145000
165964.1440425401: 170000
190858.7656489211: 195000
219487.58049625927: 220000
252410.71757069815: 255000
290272.32520630286: 295000
333813.1739872483: 335000
383885.1500853355: 385000
441467.9225981358: 445000
507688.1109878561: 510000
583841.3276360345: 585000
671417.5267814395: 675000
772130.1557986555: 775000
887949.6791684537: 890000
1021142.1310437217: 1050000
1174313.4507002798: 1200000
1350460.4683053216: 1400000
1553029.5385511199: 1600000
1785983.9693337877: 1800000
2053881.5647338557: 2100000
2361963.7994439336: 2400000
2716258.3693605233: 2750000
3123697.1247646017: 3150000
3592251.6934792916: 3600000
4131089.447501185: 4150000
4750752.864626362: 4800000
5463365.794320316: 5500000
6282870.663468363: 6300000
7225301.262988617: 7250000
8309096.452436908: 8350000
9555460.920302443: 9600000
10988780.058347808: 11000000
12637097.06709998: 13000000
14532661.627164975: 15000000
16712560.87123972: 17000000
19219445.001925677: 19500000
22102361.75221453: 22500000
25417716.015046705: 25500000
29230373.417303707: 29500000
33614929.42989926: 34000000
38657168.84438415: 39000000
44455744.171041764: 44500000
51124105.79669803: 51500000
58792721.666202724: 59000000
67611629.91613312: 68000000
77753374.40355308: 78000000
89416380.56408603: 89500000
102828837.64869893: 105000000
118253163.29600376: 120000000
135991137.79040432: 140000000
156389808.45896494: 160000000
179848279.72780967: 180000000
206825521.6869811: 210000000
237849349.94002825: 240000000
273526752.4310325: 275000000
314555765.2956873: 315000000
361739130.0900404: 365000000
415999999.60354644: 420000000
478399999.54407835: 480000000
550159999.47569: 555000000
632683999.3970435: 635000000
727586599.3066: 730000000
836724589.2025899: 840000000
962233277.5829782: 965000000
1106568269.220425: 1150000000
1272553509.6034884: 1300000000
1463436536.0440116: 1500000000
1682952016.4506133: 1700000000
1935394818.918205: 1950000000
2225704041.7559357: 2250000000
2559559648.0193257: 2600000000
2943493595.222224: 2950000000
3385017634.5055575: 3400000000
3892770279.681391: 3900000000
4476685821.633599: 4500000000
5148188694.878639: 5150000000
5920416999.110435: 5950000000
6808479548.976999: 6850000000
7829751481.323548: 7850000000
9004214203.52208: 9050000000
10354846334.05039: 10500000000
11908073284.157948: 12000000000
13694284276.78164: 14000000000
15748426918.298883: 16000000000
18110690956.043716: 18500000000
20827294599.45027: 21000000000
23951388789.36781: 24000000000
27544097107.77298: 28000000000
31675711673.938923: 32000000000
36427068425.02976: 36500000000
41891128688.784225: 42000000000
48174797992.10185: 48500000000
55401017690.91713: 55500000000
63711170344.554695: 64000000000
73267845896.2379: 73500000000
84258022780.67358: 84500000000
96896726197.77461: 97000000000
111431235127.4408: 115000000000
128145920396.5569: 130000000000
147367808456.04044: 150000000000
169472979724.4465: 170000000000
194893926683.11346: 195000000000
224128015685.58047: 225000000000
257747218038.4175: 260000000000
296409300744.1801: 300000000000
340870695855.8071: 345000000000
392001300234.17816: 395000000000
450801495269.3049: 455000000000
518421719559.70056: 520000000000
596184977493.6556: 600000000000
685612724117.704: 690000000000
788454632735.3595: 790000000000
906722827645.6633: 910000000000
1042731251792.5127: 1050000000000
1199140939561.3894: 1200000000000
1379012080495.5977: 1400000000000
1585863892569.9373: 1600000000000

About

Get a nice rounded number, easily digestible by human beings

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published