Using Javascript, I want to format a number to always display 3 digits, along with it's proper identifier (ex: Million, Thousand). If under 100,000, the number should only show the "thousands" digits
All numbers will be integers above zero, and the highest numbers will be in the trillions.
A few examples of what I'm looking for:
1 Thousand
13 Thousand
627 Thousand
2.85 Million
67.9 Million
153 Million
9.52 Billion
etc...
All of the solutions I tried ended up becoming spaghetti code, and I am hoping someone can find a clean, smart solution.
EDIT:
I ended up using part of RobG's solution, but worked out the specifics on my own. I added rounding as well
function getNumberName(num) {
var numNames = ['', 'Thousand', 'Million', 'Billion', 'Trillion'];
num = num.toString();
var len = num.length - 1;
return numNames[len / 3 | 0];
}
function test(num) {
var numStr = num.toString();
if (num < 1000) {
return numStr;
} else if (num < 1000000) {
return numStr.slice(0, -3) + "," + numStr.slice(-3);
}
numStr = Math.round(parseFloat(numStr.slice(0, 3) + "." + numStr[3])).toString() + Array(numStr.slice(3).length + 1).join("0");
var remainder = numStr.length % 3;
var before = numStr.slice(0, remainder);
var after = numStr.slice(remainder, 3);
var sep = "";
if (before.length) {
sep = ".";
}
return before + sep + after + " " + getNumberName(num);
}
var nums = [0, 1, 12, 123, 1234, 12345, 123456, 1237567, 12325678, 123856789, 123e7, 125e8, 123.6e9, 123e10];
nums.forEach(function(num) {
document.write(num + ": $" + test(num) + "<br/>");
});
For integers, you can try shortening the number to the required number of places, then adding a name, e.g.
// For integers
function getNumberName(num) {
var numNames = ['','Thousand','Million','Billion'];
var num = num.toString()
var len = num.length - 1;
var pow = numNames[len/3 | 0];
return pow;
}
// For integers
function abbreviateNumber(num, places) {
places = places || 0;
var len = num.toString().length - 1;
var pow = len/3 | 0
return (num < 1000? num : num/Math.pow(10, pow*3)).toFixed(places);
}
function getNumberAbbr(num, places) {
return abbreviateNumber(num, places) + ' ' + getNumberName(num);
}
var nums = [0,1,12,123,1234,12345,123456,1234567,12345678,123456789,123e7]
nums.forEach(function(num) {
console.log(num + ' : ' + getNumberAbbr(num,1));
});
The above is not complete. There should be a limit applied so that beyond, say 1 trillion, it stops shortening the number.
There might be a better way but this will work:
function getTextNums(number) {
if ((number/1000000) < 1) {
var n = number.toString();
var first3 = n.substring(0, 3);
console.log(first3 + "Thousand");
} else if((number/1000000000) < 1) {
var n = number.toString();
var first3 = n.substring(0, 3);
console.log(first3 + "Million");
} else if((number/1000000000000) < 1) {
var n = number.toString();
var first3 = n.substring(0, 3);
console.log(first3 + "Billion");
} else if((number/1000000000000000) < 1) {
var n = number.toString();
var first3 = n.substring(0, 3);
console.log(first3 + "Trillion");
} else {
}
}
getTextNums(4533544433000)
I've the following regular expressions that I want to run on the example input, to replace both numbers and fractions (I need to multiply them by a number that the user will choose):
numbersRegex = /[0-9]+(?:\.[0-9]*)?/g;
fractionsRegex = /((\d+)\/(\d+))/g;
The numbersRegex needs to run on lines containing numbers only (I marked them in the example input underneath using numbersRegex).
The fractionsRegex needs to run on lines containing fractions only (I marked them in the example input underneath using fractionsRegex).
I need them to be two different ones as the multiplication method is different, so I would like to run str.replace two times, one to multiply all fractions and the other to multiply all numbers.
Here is the example input:
1 1/2 oz. white rum (fractionsRegex)
1/2 lime, cut in 4 wedges (fractionsRegex)
10 mint leaves, fresh (numbersRegex)
2 tbsp. white sugar (numbersRegex)
1 cup ice cubes (numbersRegex)
1/2 cup club soda (fractionsRegex)
Is that possible?
Many thanks
UPDATE
This is the function I'm currently using (it needs to be cleaned up and I'm sure it can also be optimised):
function _increaseServings(target, initialServings, newServings) {
target.html(originalIngredientsHTML);
target.find('li').each(function(i) {
var currentLine = $(this).text();
var importantPart = currentLine.split(',');
var fractionsRegex = importantPart[0].match(/((\d+)\/(\d+))/g);
var lineHiddenFractions = importantPart[0] != null ? importantPart[0].replace(/([0-9]{1,})([\/]{1})([0-9]{1,})/g, "") : "";
var numbersRegex = lineHiddenFractions.match(/([0-9]+(?:\.[0-9]*)?)/g);
var result = {};
var strToReplace = '';
if (fractionsRegex == null && numbersRegex == null) return;
if (fractionsRegex !== null && fractionsRegex.length > 0) {
result.fraction = fractionsRegex[0];
strToReplace = result.fraction;
}
if (numbersRegex !== null && numbersRegex.length > 0) {
result.number = parseInt(numbersRegex[0]);
if(result.fraction) {
strToReplace = result.number + ' ' + strToReplace;
} else {
strToReplace = result.number;
}
}
if(result.fraction) {
var fraction = result.fraction.split('/');
if(result.number && result.fraction) {
result.decimal = parseInt(result.number) + parseInt(fraction[0]) / parseInt(fraction[1]);
} else {
result.decimal = parseInt(fraction[0]) / parseInt(fraction[1]);
}
} else {
result.decimal = parseInt(result.number);
}
result.stringToReplace = strToReplace;
var newValue = result.decimal * (newServings / initialServings);
if(newValue % 1 != 0) {
var values = String(newValue).split('.');
var checkValue = String(values[1]).slice(0,1);
var integerPart = Math.floor(newValue);
if(checkValue == 2) {
// 25
newValue = integerPart + ' 1/4';
} else if(checkValue == 3) {
// 33
newValue = integerPart + ' 1/3';
} else if(checkValue == 5) {
// 50
newValue = integerPart + ' 1/2';
} else if(checkValue == 6) {
// 66
newValue = integerPart + ' 2/3';
} else if(checkValue == 7) {
// 75
newValue = integerPart + ' 3/4';
}
if(integerPart == 0) newValue = newValue.slice(2, newValue.length);
}
currentLine = currentLine.replace(strToReplace, newValue);
$(this).text(currentLine);
});
}
Unfortunately, javascript regular expressions don't support negative lookbehinds, so you will get false positives on your fraction numbers when you are looking for just numbers. There's no way to say something like...
"/(?<!\/)([0-9])/"
...in javascript, so you will end up picking up some of the digits from the fraction.
You have to fake out the results with something like this:
Assuming your text is terminated with \n just for testing purposes:
var t = "1 1/2 oz. white rum (fractionsRegex)\n1/2 lime, cut in 4 wedges (fractionsRegex)\n10 mint leaves, fresh (numbersRegex)\n2 tbsp. white sugar (numbersRegex)\n1 cup ice cubes (numbersRegex)\n1/2 cup club soda (fractionsRegex)";
var ary = t.split("\n");
for (var i = 0; i < ary.length; i++) {
var fractionsRegex = ary[i].match(/((\d+)\/(\d+))/g);
var lineHiddenFractions = ary[i] != null ? ary[i].replace(/([0-9]{1,})([\/]{1})([0-9]{1,})/g, "") : "";
var numbersRegex = lineHiddenFractions.match(/([0-9]+(?:\.[0-9]*)?)/g);
if (fractionsRegex !== null && fractionsRegex.length > 0) {
// We have a fraction for this line.
for (var j = 0; j < fractionsRegex.length; j++) {
alert("Fraction on line " + i + ": " + fractionsRegex[j]);
}
}
if (numbersRegex !== null && numbersRegex.length > 0) {
// We have a number for this line.
for (var k = 0; k < numbersRegex.length; k++) {
alert("Number on line " + i + ": " + numbersRegex[k]);
}
}
}
I think this method offers you the most flexibility for what you want to do. For each line, you will get an array of all of your fractions and an array of all of your numbers. You can place booleans to check if the line has both numbers and fractions or just one or the other. If you need to perform calculations, you can create formulas from the arrays of fractions and numbers from each line.
to calculate
function measure(string){
var match = string.match(/^\d+(\s*\d*(\/+\d+))*/);
if(match){
match = match[0].split(" ");
var m = 0;
while(match.length){
m += eval(match.shift());
}
return m;
}
return 0;
}
or to get the values separately
function get(string){
var match = string.match(/^\d+(\s*\d*(\/+\d+))*/);
if(match){
match = match[0].split(" ");
var m = 0;
while(match.length){
m += eval(match.shift());
}
return {integer:Math.floor(m), fraction:m-Math.floor(m)};
}
return {integer:0, fraction:0};
}
or to replace integer part and fraction part seperately
function replaceInteger(string, newInt){
return string.replace(/^\d+\s+/, function(m, n){
return newInt + " ";
});
}
function replaceFraction(string, newFract){
return string.replace(/^\d+\/+\d+\s+/, function(m, n){
return newFract + " ";
});
}
replaceInteger("10 mint leaves, fresh", "100");
replaceFraction("1/2 lime leaves, fresh", "1/3");
I think this approach will do what you need. Just call getQuantity, it will return an object which may or may not contain properties number and fraction. Then once you get the result, you can perform logic to determine what to do with it.
function getQuantity(recipe){
var regex = /^(([1-9]+\d*\s)?((\d+)\/(\d+)\s)?).*$/g;
var results = regex.exec(recipe);
if(results[2] && results[5]){
return {number: Number(results[2]), fraction: Number(results[4]) / Number(results[5])};
} if(results[5]){
return {fraction: Number(results[4]) / Number(results[5])};
} else{
return {number: Number(results[2])};
}
}
getQuantity("1 1/2 oz. white rum"); // {number: 1, fraction: 0.5}
getQuantity("1/2 lime, cut in 4 wedges"); // {fraction: 0.5}
getQuantity("10 mint leaves, fresh"); // {number: 10}
getQuantity("2 tbsp. white sugar"); // {number: 2}
getQuantity("1 cup ice cubes"); // {number: 1}
getQuantity("1/2 cup club soda"); // {fraction: 0.5}
In JavaScript, how can I convert a sequence of numbers in an array to a range of numbers? In other words, I want to express consecutive occurring integers (no gaps) as hyphenated ranges.
[2,3,4,5,10,18,19,20] would become [2-5,10,18-20]
[1,6,7,9,10,12] would become [1,6-7,9-10,12]
[3,5,99] would remain [3,5,99]
[5,6,7,8,9,10,11] would become [5-11]
Here is an algorithm that I made some time ago, originally written for C#, now I ported it to JavaScript:
function getRanges(array) {
var ranges = [], rstart, rend;
for (var i = 0; i < array.length; i++) {
rstart = array[i];
rend = rstart;
while (array[i + 1] - array[i] == 1) {
rend = array[i + 1]; // increment the index if the numbers sequential
i++;
}
ranges.push(rstart == rend ? rstart+'' : rstart + '-' + rend);
}
return ranges;
}
getRanges([2,3,4,5,10,18,19,20]);
// returns ["2-5", "10", "18-20"]
getRanges([1,2,3,5,7,9,10,11,12,14 ]);
// returns ["1-3", "5", "7", "9-12", "14"]
getRanges([1,2,3,4,5,6,7,8,9,10])
// returns ["1-10"]
Just having fun with solution from CMS :
function getRanges (array) {
for (var ranges = [], rend, i = 0; i < array.length;) {
ranges.push ((rend = array[i]) + ((function (rstart) {
while (++rend === array[++i]);
return --rend === rstart;
})(rend) ? '' : '-' + rend));
}
return ranges;
}
Very nice question: here's my attempt:
function ranges(numbers){
var sorted = numbers.sort(function(a,b){return a-b;});
var first = sorted.shift();
return sorted.reduce(function(ranges, num){
if(num - ranges[0][1] <= 1){
ranges[0][1] = num;
} else {
ranges.unshift([num,num]);
}
return ranges;
},[[first,first]]).map(function(ranges){
return ranges[0] === ranges[1] ?
ranges[0].toString() : ranges.join('-');
}).reverse();
}
Demo on JSFiddler
I needed TypeScript code today to solve this very problem -- many years after the OP -- and decided to try a version written in a style more functional than the other answers here. Of course, only the parameter and return type annotations distinguish this code from standard ES6 JavaScript.
function toRanges(values: number[],
separator = '\u2013'): string[] {
return values
.slice()
.sort((p, q) => p - q)
.reduce((acc, cur, idx, src) => {
if ((idx > 0) && ((cur - src[idx - 1]) === 1))
acc[acc.length - 1][1] = cur;
else acc.push([cur]);
return acc;
}, [])
.map(range => range.join(separator));
}
Note that slice is necessary because sort sorts in place and we can't change the original array.
Here's my take on this...
function getRanges(input) {
//setup the return value
var ret = [], ary, first, last;
//copy and sort
var ary = input.concat([]);
ary.sort(function(a,b){
return Number(a) - Number(b);
});
//iterate through the array
for (var i=0; i<ary.length; i++) {
//set the first and last value, to the current iteration
first = last = ary[i];
//while within the range, increment
while (ary[i+1] == last+1) {
last++;
i++;
}
//push the current set into the return value
ret.push(first == last ? first : first + "-" + last);
}
//return the response array.
return ret;
}
Using ES6, a solution is:
function display ( vector ) { // assume vector sorted in increasing order
// display e.g.vector [ 2,4,5,6,9,11,12,13,15 ] as "2;4-6;9;11-13;15"
const l = vector.length - 1; // last valid index of vector array
// map [ 2,4,5,6,9,11,12,13,15 ] into array of strings (quote ommitted)
// --> [ "2;", "4-", "-", "6;", "9;", "11-", "-", "13;", "15;" ]
vector = vector.map ( ( n, i, v ) => // n is current number at index i of vector v
i < l && v [ i + 1 ] - n === 1 ? // next number is adjacent ?
`${ i > 0 && n - v [ i - 1 ] === 1 ? "" : n }-` :
`${ n };`
);
return vector.join ( "" ). // concatenate all strings in vector array
replace ( /-+/g, "-" ). // replace multiple dashes by single dash
slice ( 0, -1 ); // remove trailing ;
}
If you want to add extra spaces for readability, just add extra calls to string.prototype.replace().
If the input vector is not sorted, you can add the following line right after the opening brace of the display() function:
vector.sort ( ( a, b ) => a - b ); // sort vector in place, in increasing order.
Note that this could be improved to avoid testing twice for integer adjacentness (adjacenthood? I'm not a native English speaker;-).
And of course, if you don't want a single string as output, split it with ";".
Rough outline of the process is as follows:
Create an empty array called ranges
For each value in sorted input array
If ranges is empty then insert the item {min: value, max: value}
Else if max of last item in ranges and the current value are consecutive then set max of last item in ranges = value
Else insert the item {min: value, max: value}
Format the ranges array as desired e.g. by combining min and max if same
The following code uses Array.reduce and simplifies the logic by combining step 2.1 and 2.3.
function arrayToRange(array) {
return array
.slice()
.sort(function(a, b) {
return a - b;
})
.reduce(function(ranges, value) {
var lastIndex = ranges.length - 1;
if (lastIndex === -1 || ranges[lastIndex].max !== value - 1) {
ranges.push({ min: value, max: value });
} else {
ranges[lastIndex].max = value;
}
return ranges;
}, [])
.map(function(range) {
return range.min !== range.max ? range.min + "-" + range.max : range.min.toString();
});
}
console.log(arrayToRange([2, 3, 4, 5, 10, 18, 19, 20]));
If you simply want a string that represents a range, then you'd find the mid-point of your sequence, and that becomes your middle value (10 in your example). You'd then grab the first item in the sequence, and the item that immediately preceded your mid-point, and build your first-sequence representation. You'd follow the same procedure to get your last item, and the item that immediately follows your mid-point, and build your last-sequence representation.
// Provide initial sequence
var sequence = [1,2,3,4,5,6,7,8,9,10];
// Find midpoint
var midpoint = Math.ceil(sequence.length/2);
// Build first sequence from midpoint
var firstSequence = sequence[0] + "-" + sequence[midpoint-2];
// Build second sequence from midpoint
var lastSequence = sequence[midpoint] + "-" + sequence[sequence.length-1];
// Place all new in array
var newArray = [firstSequence,midpoint,lastSequence];
alert(newArray.join(",")); // 1-4,5,6-10
Demo Online: http://jsbin.com/uvahi/edit
; For all cells of the array
;if current cell = prev cell + 1 -> range continues
;if current cell != prev cell + 1 -> range ended
int[] x = [2,3,4,5,10,18,19,20]
string output = '['+x[0]
bool range = false; --current range
for (int i = 1; i > x[].length; i++) {
if (x[i+1] = [x]+1) {
range = true;
} else { //not sequential
if range = true
output = output || '-'
else
output = output || ','
output.append(x[i]','||x[i+1])
range = false;
}
}
Something like that.
An adaptation of CMS's javascript solution for Cold Fusion
It does sort the list first so that 1,3,2,4,5,8,9,10 (or similar) properly converts to 1-5,8-10.
<cfscript>
function getRanges(nArr) {
arguments.nArr = listToArray(listSort(arguments.nArr,"numeric"));
var ranges = [];
var rstart = "";
var rend = "";
for (local.i = 1; i <= ArrayLen(arguments.nArr); i++) {
rstart = arguments.nArr[i];
rend = rstart;
while (i < ArrayLen(arguments.nArr) and (val(arguments.nArr[i + 1]) - val(arguments.nArr[i])) == 1) {
rend = val(arguments.nArr[i + 1]); // increment the index if the numbers sequential
i++;
}
ArrayAppend(ranges,rstart == rend ? rstart : rstart & '-' & rend);
}
return arraytolist(ranges);
}
</cfscript>
Tiny ES6 module for you guys. It accepts a function to determine when we must break the sequence (breakDetectorFunc param - default is the simple thing for integer sequence input).
NOTICE: since input is abstract - there's no auto-sorting before processing, so if your sequence isn't sorted - do it prior to calling this module
function defaultIntDetector(a, b){
return Math.abs(b - a) > 1;
}
/**
* #param {Array} valuesArray
* #param {Boolean} [allArraysResult=false] if true - [1,2,3,7] will return [[1,3], [7,7]]. Otherwise [[1.3], 7]
* #param {SequenceToIntervalsBreakDetector} [breakDetectorFunc] must return true if value1 and value2 can't be in one sequence (if we need a gap here)
* #return {Array}
*/
const sequenceToIntervals = function (valuesArray, allArraysResult, breakDetectorFunc) {
if (!breakDetectorFunc){
breakDetectorFunc = defaultIntDetector;
}
if (typeof(allArraysResult) === 'undefined'){
allArraysResult = false;
}
const intervals = [];
let from = 0, to;
if (valuesArray instanceof Array) {
const cnt = valuesArray.length;
for (let i = 0; i < cnt; i++) {
to = i;
if (i < cnt - 1) { // i is not last (to compare to next)
if (breakDetectorFunc(valuesArray[i], valuesArray[i + 1])) {
// break
appendLastResult();
}
}
}
appendLastResult();
} else {
throw new Error("input is not an Array");
}
function appendLastResult(){
if (isFinite(from) && isFinite(to)) {
const vFrom = valuesArray[from];
const vTo = valuesArray[to];
if (from === to) {
intervals.push(
allArraysResult
? [vFrom, vTo] // same values array item
: vFrom // just a value, no array
);
} else if (Math.abs(from - to) === 1) { // sibling items
if (allArraysResult) {
intervals.push([vFrom, vFrom]);
intervals.push([vTo, vTo]);
} else {
intervals.push(vFrom, vTo);
}
} else {
intervals.push([vFrom, vTo]); // true interval
}
from = to + 1;
}
}
return intervals;
};
module.exports = sequenceToIntervals;
/** #callback SequenceToIntervalsBreakDetector
#param value1
#param value2
#return bool
*/
first argument is the input sequence sorted array, second is a boolean flag controlling the output mode: if true - single item (outside the intervals) will be returned as arrays anyway: [1,7],[9,9],[10,10],[12,20], otherwise single items returned as they appear in the input array
for your sample input
[2,3,4,5,10,18,19,20]
it will return:
sequenceToIntervals([2,3,4,5,10,18,19,20], true) // [[2,5], [10,10], [18,20]]
sequenceToIntervals([2,3,4,5,10,18,19,20], false) // [[2,5], 10, [18,20]]
sequenceToIntervals([2,3,4,5,10,18,19,20]) // [[2,5], 10, [18,20]]
Here's a version in Coffeescript
getRanges = (array) ->
ranges = []
rstart
rend
i = 0
while i < array.length
rstart = array[i]
rend = rstart
while array[i + 1] - array[i] is 1
rend = array[i + 1] # increment the index if the numbers sequential
i = i + 1
if rstart == rend
ranges.push rstart + ''
else
ranges.push rstart + '-' + rend
i = i + 1
return ranges
I've written my own method that's dependent on Lo-Dash, but doesn't just give you back an array of ranges, rather, it just returns an array of range groups.
[1,2,3,4,6,8,10] becomes:
[[1,2,3,4],[6,8,10]]
http://jsfiddle.net/mberkom/ufVey/