Get the number and round to whole number - javascript

I'm getting the miles from Google Map API V3 and displaying it in a textbox based on the from and to addresses. I want to parse out the number and round it off to the nearest whole number.
Miles = "29.9 mi" // i want the result as 30
Miles = "9.3 mi" // 10
I tried Math.round but it's returning NAN.
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var miles = response.routes[0].legs[0].distance.text;
miles = Math.round(miles);
$('#TotalMiles').val(miles);
}

Did you look at the JavaScript API for round?
Syntax
Math.round(x)
Parameters
x A number.
Description
If the fractional portion of number is .5 or greater, the argument is
rounded to the next higher integer. If the fractional portion of
number is less than .5, the argument is rounded to the next lower
integer.
Because round is a static method of Math, you always use it as
Math.round(), rather than as a method of a Math object you created.
EDIT:
You need to convert the string into its parts to get the number
Miles = "29.9 mi"
var num = parseFloat(Miles);
Math.round(num);
and way to do it with a regular expression
Miles = "29 mi"
var rounded = Miles.replace(/(\d+(\.\d+)?)(\s?mi)/, function(match, number, x, units){ return Math.round(parseFloat(number)) + units });
console.log(rounded);

Try parseFloat + Math.ceil:
var number = "9.3 mi";
console.log(Math.ceil(parseFloat(number)));

you may need to split out the extra characters.
var str = "29.9 mi";
var number = str.split(" ");
var new_number = Math.round(parseFloat(number[0]));
This is a longwinded answer. In the end it would make more sense to tailor it to what you need exactly, but here I am just showing how to split on " " (space) and round the resulting string after parsing it as a float.
I would try KaeruCT's answer though. I don't think parseInt or parseFloat actually need the non numeric characters removed.

Try Math.round():
var number =29.9;
console.log(Math.round(number));

Related

Multiply points of a string

Let's say that I have a list of points declared in this format: x1,y1 x2,y2
listOfPoints : string = "12.2, 13.0 198.2, 141";
What could I do to multiply by 1.5, for example, every number of this string ?
Do I need to iterate over the listOfPoints and extract a string every time that there is a ', ' or ' ', convert that string into a number, multiply it by 1.5 and reconvert it into a string to finally put it into a new string (newListOfPoints) ?
Or is there a different way to that more efficiently ?
Thank you.
Use a regular expression with a replacer function to match digits, possibly with decimals, and replace with those digits multiplied by the number you want:
const listOfPoints = "12.2, 13.0 198.2, 141";
const multiplied = listOfPoints.replace(
/\d+(?:\.\d+)?/g,
match => match * 1.5
);
console.log(multiplied);
Due to floating-point issues, some of the resulting numbers may have trailing digits. If you don't want that, you can round the multiplied number to a certain number of decimal places:
const listOfPoints = "12.2, 13.0 198.2, 141";
const multiplied = listOfPoints.replace(
/\d+(?:\.\d+)?/g,
match => Math.round(1000 * match * 1.5) / 1000
);
console.log(multiplied);

Parsing toLocaleString and back to float loosing precision

This is what works as expected:
This parseFloat(newValue).toLocaleString("de-DE", { minimumFractionDigits: 2, maximumFractionDigits: 2 }) will result in a String: "34.886,55"
This does not work as expected:
For parseFloat("34.886,55") I get a Number, but I lost everything after the comma: 34.886.
How can I fix this problem ?
I wrote this simple function for doing locale based parseFloat, to the best of my knowledge, the assumptions for this function are.
The decimal operator will come once.
So split it at the decimal("," in our case) to get two elements in an array.
Then remove the thousands separator("." in our case) for each of the elements in the array.
Then joinback the array with the decimal separator ("." in our case)
finally return the parseFloat value of this joined number!
var str = "34.886,55"
console.log("before: ", str);
str = localeParseFloat(str, ",", ".");
console.log("resulting floating value from the function: ", str);
function localeParseFloat(str, decimalChar, separatorChar){
var out = [];
str.split(",").map(function(x){
x = x.replace(".", "");
out.push(x);
})
out = out.join(".");
return parseFloat(out);
}
<script src="https://rawgit.com/eu81273/jsfiddle-console/master/console.js"></script>
Here's a brief example of the number parsing... Commas are not valid in numbers, only the full stop represents the decimal separator. If it is encountered, the string conversion stop, as you can see on the second log. Also check the third log, I've added a check for the value to be true if it's lower than 100, making it more obvious that you are not playing with thousand separator but really decimal separator.
console.log(parseFloat("34.886.55"));
console.log(parseFloat("34,886.55"));
console.log(parseFloat("34.886,55"), parseFloat("34.886,55") < 100);

parseInt() doesn't regard leading zeros, even with radix

When I want to convert a binary string to a base 10 decimal (Like this: parseInt('0001010', 2)), Javascript returns a decimal number, but a version in which the leading zeros mentioned in the example above have been disregarded. Is there any way to fix this?
So supposing you have the number '00000101101101':
var number = '00000101101101';
var length = number.length;
var decimal_number = parseInt(number, 2);
// going back
var new_number = decimal_number.toString(2);
var new_length = new_number.length;
var n_zeros = length - new_length;
var zeros = (n_zeros >= 2 ? Array(n_zeros+1).join("0") : "0");
new_number = zeros + new_number;
The decimal representation has no way to keep track of leading zeros. If you wish to keep the leading zeros in the result, you need a fundamentally different approach (e.g. keeping the output as a string).
Alternatively, if you know the width of the result a priori, you could just pad it with leading zeros on output.

Javascript: Convert a string representation of money to Number

Lets say I have an amount in string format like this:
amount = '12,000.00'
I want to convert it into a Number (Javascript) or a float.
parseFloat(amount) // this gives me 12 as a result
Number(amount) // this gives me NaN as a result
Other solution I thought was this:
parseFloat(amount.replace(/[,]/g, ''))
This works fine. But the problem here is the Locale.
This would fail when the amount is € 12000,00.
Here ',' has altogether a different meaning.
I looked around for a good solution but couldn't. I am looking for a generalized solution.
This is not that easy, as you can't exactly know what's the delimiter for thousands and what for the decimal part
Consider "12.000.000" is it 12000.000 === 12000 or 12000000?
But if you would set the requirement that the last delimiter is always the decimal delimiter -
meaning if at least one delimiter is given, the last one has to be the decimal delimiter, *if the digits following, don't exceed a defined length.
Then you could try the following
Edit
(see the revs if you're interested in the old function)
I put in the ability to define the max length of digits after the last delimiter "," or "." up until it is treated as float, after that its returned as integer
var amounts = ["12000","12.000,00", "12,000.00", "12,000,01", "12.000.02", "12,000,001"];
formatMoney.maxDecLength = 3; //Set to Infinity o.s. to disable it
function formatMoney(a) {
var nums = a.split(/[,\.]/);
var ret = [nums.slice(0, nums.length - 1).join("")];
if (nums.length < 2) return +nums[0];
ret.push(nums[nums.length - 1]);
return +(ret.join(nums[nums.length - 1].length < formatMoney.maxDecLength ? "." : ""));
}
for ( var i=0,j;j=amounts[i];i++)
console.log (j + " -> " +formatMoney(j));
Gives the output:
"12000 -> 12000"
"12.000,00 -> 12000"
"12,000.00 -> 12000"
"12,000,01 -> 12000.01"
"12.000.02 -> 12000.02"
"12,000,001 -> 12000001" //as you can see after the last "," there are 3 digits and its treated as integer
Another JSBin
You can get the local decimal delimiter in this manner:
1.1.toLocaleString().substr(1,1)
Before parse float, you could make sure the string contains nothing but numbers, possibly a minus sign, and the local decimal delimiter.
The truth is, you'll never know the format. 12,345. Is that 12345, or another locale version if 12.345?
However, if you have consistent decimals, then you'd be able to use the lastIndexOf function on a comma and a period will reveal the decimal position and character.
var price = '12,345.67';
var lastPeriod = price.lastIndexOf('.');
var lastComma = price.lastIndexOf(',');
if (lastComma != -1 && lastComma > lastPeriod) {
decimalCharacter = ',';
} else {
decimalCharacter = '.';
}
console.log(decimalCharacter); //. or , based on how the price string looks - see below
If price is 12,345.67, decimalCharacter will be .. If it's 12.345,67, it'll be returned as ,.

Truncate decimal number (as strings) to a fixed number of places

I want to truncate numbers (given as strings) to a fixed number of decimal places. The numbers can be negative (with a minus sign), positive (no sign). I'd prefer to round the numbers properly and keep trailing zeroes. I want the same number of decimal places, no matter how long the whole number is. The numbers will be stored back as strings.
For example:
140.234234234 -> 140.234
1.123123 -> 1.123
-12.789789 -> -12.790
First parse them as floats, then format with toFixed:
var nums = [
"140.234234234", // -> 140.234
"1.123123", // -> 1.123
"-12.789789" // -> -12.790
];
nums.forEach(function(n) {
console.log(parseFloat(n).toFixed(3));
});
http://codepen.io/anon/pen/IvxmA
Any number can be displayed as a fixed decimal place string by using .toFixed:
var num = 140.234234234;
var fixedDecimalPlace = num.toFixed(3); // is "140.234"
function truncate( numberString, trunk ) {
var onpoint = numberString.split('.',2);
var numberStringTruncated = numberString;
if (onpoint.length > 1) {
numberStringTruncated = onpoint[0] + '.' + onpoint[1].substring(0,trunk);
}
return numberStringTruncated;
}
This does not consider rounding or padding. As the other answer suggested, you should use parseFloat followed by toFixed

Categories

Resources