How to extract digits after a comma [duplicate] - javascript

I am validating a decimal number using JavaScript.
Am just using NaN
var a = 12345.67
Is there any javascript function to get the count or the value itself before and after decimal point .
before() should return 1234
after() should return 67
Please dont suggest a substring!

var a = 12345.67;
alert(a.toString().split(".")[0]); ///before
alert(a.toString().split(".")[1]); ///after
Here is a simple fiddle http://jsfiddle.net/qWtSc/
zzzzBov's suggestion is this
Number.prototype.before = function () {
var value = parseInt(this.toString().split(".")[0], 10);//before
return value ? value : 0;
}
Number.prototype.after = function () {
var value = parseInt(this.toString().split(".")[1], 10);//after
return value ? value : 0;
}
Usage
alert(a.before()); ///before
alert(a.after()); ///after

before is easy. It's just a round down operation.
var before = function(n) {
return Math.floor(n);
};
after is harder without string processing. I mean how would you handle after(Math.PI)? You can't hold a integer with an infinite number of digits after all.
But with some string processing it's easy, just know it won't be exact due to the wonders of floating point math.
var after = function(n) {
var fraction = n.toString().split('.')[1];
return parseInt(fraction, 10);
};

Playing off of other answers... and you wanted a 'numeric' version.. still easiest to convert it to a string and work off the split function...
function getNatural(num) {
return parseFloat(num.toString().split(".")[0]);
}
function getDecimal(num) {
return parseFloat(num.toString().split(".")[1]);
}
var a = 12345.67;
alert(getNatural(a)); ///before
alert(getDecimal(a)); ///after
http://jsfiddle.net/rlemon/qWtSc/1/

var decimalPlaces = 2;
var num = 12345.673
var roundedDecimal = num.toFixed(decimalPlaces);
var intPart = Math.floor(roundedDecimal);
var fracPart = parseInt((roundedDecimal - intPart), 10);
//or
var fractPart = (roundedDecimal - intPart) * Math.pow(10, decimalPlaces);

To find the count/length of characters after dot:
var a = 12345.67;
var after_dot = (a.toString().split(".")[1]).length;
var before_dot= (a.toString().split(".")[0]).length;

Unfortunately there's no way to get the factional part in a reliable way using math functions, because pretty odd roundings often occur, depending on the Javascript engine used.
The best thing to do is to convert it to a string, and then checking if the results is in decimal or scientific notation.
Number.prototype.after = function() {
var string = this.toString();
var epos = string.indexOf("e");
if (epos === -1) { // Decimal notation
var i = string.indexOf(".");
return i === -1 ? "" : n.substring(i + 1);
}
// Scientific notation
var exp = string.substring(epos + 1) - 0; // this is actually faster
// than parseInt in many browsers
var mantix = n.string.substring(0, epos).replace(".", "");
if (exp >= -1) return mantix.substring(exp + 1);
for (; exp < -1; exp++) mantix = "0" + mantix;
return mantix;
}

If your digits after decimal point are fixed, then this solution works without converting to string.
This example shows a solution for 2 digits after decimal.
Before decimal:
const wholeNum = Math.floor(num);
After decimal:
let decimal = (num - wholeNum) * 100

Related

NaN (not a number) when attempting output 2 decimal place for money value [duplicate]

I have a text box that will have a currency string in it that I then need to convert that string to a double to perform some operations on it.
"$1,100.00" → 1100.00
This needs to occur all client side. I have no choice but to leave the currency string as a currency string as input but need to cast/convert it to a double to allow some mathematical operations.
Remove all non dot / digits:
var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9.-]+/g,""));
accounting.js is the way to go. I used it at a project and had very good experience using it.
accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
accounting.unformat("€ 1.000.000,00", ","); // 1000000
You can find it at GitHub
Use a regex to remove the formating (dollar and comma), and use parseFloat to convert the string to a floating point number.`
var currency = "$1,100.00";
currency.replace(/[$,]+/g,"");
var result = parseFloat(currency) + .05;
I know this is an old question but wanted to give an additional option.
The jQuery Globalize gives the ability to parse a culture specific format to a float.
https://github.com/jquery/globalize
Given a string "$13,042.00", and Globalize set to en-US:
Globalize.culture("en-US");
You can parse the float value out like so:
var result = Globalize.parseFloat(Globalize.format("$13,042.00", "c"));
This will give you:
13042.00
And allows you to work with other cultures.
I know this is an old question, but CMS's answer seems to have one tiny little flaw: it only works if currency format uses "." as decimal separator.
For example, if you need to work with russian rubles, the string will look like this:
"1 000,00 rub."
My solution is far less elegant than CMS's, but it should do the trick.
var currency = "1 000,00 rub."; //it works for US-style currency strings as well
var cur_re = /\D*(\d+|\d.*?\d)(?:\D+(\d{2}))?\D*$/;
var parts = cur_re.exec(currency);
var number = parseFloat(parts[1].replace(/\D/,'')+'.'+(parts[2]?parts[2]:'00'));
console.log(number.toFixed(2));
Assumptions:
currency value uses decimal notation
there are no digits in the string that are not a part of the currency value
currency value contains either 0 or 2 digits in its fractional part *
The regexp can even handle something like "1,999 dollars and 99 cents", though it isn't an intended feature and it should not be relied upon.
Hope this will help someone.
This example run ok
var currency = "$1,123,456.00";
var number = Number(currency.replace(/[^0-9\.]+/g,""));
console.log(number);
For anyone looking for a solution in 2021 you can use Currency.js.
After much research this was the most reliable method I found for production, I didn't have any issues so far. In addition it's very active on Github.
currency(123); // 123.00
currency(1.23); // 1.23
currency("1.23") // 1.23
currency("$12.30") // 12.30
var value = currency("123.45");
currency(value); // 123.45
typescript
import currency from "currency.js";
currency("$12.30").value; // 12.30
This is my function. Works with all currencies..
function toFloat(num) {
dotPos = num.indexOf('.');
commaPos = num.indexOf(',');
if (dotPos < 0)
dotPos = 0;
if (commaPos < 0)
commaPos = 0;
if ((dotPos > commaPos) && dotPos)
sep = dotPos;
else {
if ((commaPos > dotPos) && commaPos)
sep = commaPos;
else
sep = false;
}
if (sep == false)
return parseFloat(num.replace(/[^\d]/g, ""));
return parseFloat(
num.substr(0, sep).replace(/[^\d]/g, "") + '.' +
num.substr(sep+1, num.length).replace(/[^0-9]/, "")
);
}
Usage : toFloat("$1,100.00") or toFloat("1,100.00$")
// "10.000.500,61 TL" price_to_number => 10000500.61
// "10000500.62" number_to_price => 10.000.500,62
JS FIDDLE: https://jsfiddle.net/Limitlessisa/oxhgd32c/
var price="10.000.500,61 TL";
document.getElementById("demo1").innerHTML = price_to_number(price);
var numberPrice="10000500.62";
document.getElementById("demo2").innerHTML = number_to_price(numberPrice);
function price_to_number(v){
if(!v){return 0;}
v=v.split('.').join('');
v=v.split(',').join('.');
return Number(v.replace(/[^0-9.]/g, ""));
}
function number_to_price(v){
if(v==0){return '0,00';}
v=parseFloat(v);
v=v.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
v=v.split('.').join('*').split(',').join('.').split('*').join(',');
return v;
}
You can try this
var str = "$1,112.12";
str = str.replace(",", "");
str = str.replace("$", "");
console.log(parseFloat(str));
let thousands_seps = '.';
let decimal_sep = ',';
let sanitizeValue = "R$ 2.530,55".replace(thousands_seps,'')
.replace(decimal_sep,'.')
.replace(/[^0-9.-]+/, '');
// Converting to float
// Result 2530.55
let stringToFloat = parseFloat(sanitizeValue);
// Formatting for currency: "R$ 2.530,55"
// BRL in this case
let floatTocurrency = Number(stringToFloat).toLocaleString('pt-BR', {style: 'currency', currency: 'BRL'});
// Output
console.log(stringToFloat, floatTocurrency);
I know you've found a solution to your question, I just wanted to recommend that maybe you look at the following more extensive jQuery plugin for International Number Formats:
International Number Formatter
How about simply
Number(currency.replace(/[^0-9-]+/g,""))/100;
Works with all currencies and locales. replaces all non-numeric chars (you can have €50.000,00 or $50,000.00) input must have 2 decimal places
jQuery.preferCulture("en-IN");
var price = jQuery.format(39.00, "c");
output is: Rs. 39.00
use jquery.glob.js,
jQuery.glob.all.js
Here's a simple function -
function getNumberFromCurrency(currency) {
return Number(currency.replace(/[$,]/g,''))
}
console.log(getNumberFromCurrency('$1,000,000.99')) // 1000000.99
For currencies that use the ',' separator mentioned by Quethzel Diaz
Currency is in Brazilian.
var currency_br = "R$ 1.343,45";
currency_br = currency_br.replace('.', "").replace(',', '.');
var number_formated = Number(currency_br.replace(/[^0-9.-]+/g,""));
var parseCurrency = function (e) {
if (typeof (e) === 'number') return e;
if (typeof (e) === 'string') {
var str = e.trim();
var value = Number(e.replace(/[^0-9.-]+/g, ""));
return str.startsWith('(') && str.endsWith(')') ? -value: value;
}
return e;
}
This worked for me and covers most edge cases :)
function toFloat(num) {
const cleanStr = String(num).replace(/[^0-9.,]/g, '');
let dotPos = cleanStr.indexOf('.');
let commaPos = cleanStr.indexOf(',');
if (dotPos < 0) dotPos = 0;
if (commaPos < 0) commaPos = 0;
const dotSplit = cleanStr.split('.');
const commaSplit = cleanStr.split(',');
const isDecimalDot = dotPos
&& (
(commaPos && dotPos > commaPos)
|| (!commaPos && dotSplit[dotSplit.length - 1].length === 2)
);
const isDecimalComma = commaPos
&& (
(dotPos && dotPos < commaPos)
|| (!dotPos && commaSplit[commaSplit.length - 1].length === 2)
);
let integerPart = cleanStr;
let decimalPart = '0';
if (isDecimalComma) {
integerPart = commaSplit[0];
decimalPart = commaSplit[1];
}
if (isDecimalDot) {
integerPart = dotSplit[0];
decimalPart = dotSplit[1];
}
return parseFloat(
`${integerPart.replace(/[^0-9]/g, '')}.${decimalPart.replace(/[^0-9]/g, '')}`,
);
}
toFloat('USD 1,500.00'); // 1500
toFloat('USD 1,500'); // 1500
toFloat('USD 500.00'); // 500
toFloat('USD 500'); // 500
toFloat('EUR 1.500,00'); // 1500
toFloat('EUR 1.500'); // 1500
toFloat('EUR 500,00'); // 500
toFloat('EUR 500'); // 500
Such a headache and so less consideration to other cultures for nothing...
here it is folks:
let floatPrice = parseFloat(price.replace(/(,|\.)([0-9]{3})/g,'$2').replace(/(,|\.)/,'.'));
as simple as that.
$ 150.00
Fr. 150.00
€ 689.00
I have tested for above three currency symbols .You can do it for others also.
var price = Fr. 150.00;
var priceFloat = price.replace(/[^\d\.]/g, '');
Above regular expression will remove everything that is not a digit or a period.So You can get the string without currency symbol but in case of " Fr. 150.00 " if you console for output then you will get price as
console.log('priceFloat : '+priceFloat);
output will be like priceFloat : .150.00
which is wrong so you check the index of "." then split that and get the proper result.
if (priceFloat.indexOf('.') == 0) {
priceFloat = parseFloat(priceFloat.split('.')[1]);
}else{
priceFloat = parseFloat(priceFloat);
}
function NumberConvertToDecimal (number) {
if (number == 0) {
return '0.00';
}
number = parseFloat(number);
number = number.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1");
number = number.split('.').join('*').split('*').join('.');
return number;
}
This function should work whichever the locale and currency settings :
function getNumPrice(price, decimalpoint) {
var p = price.split(decimalpoint);
for (var i=0;i<p.length;i++) p[i] = p[i].replace(/\D/g,'');
return p.join('.');
}
This assumes you know the decimal point character (in my case the locale is set from PHP, so I get it with <?php echo cms_function_to_get_decimal_point(); ?>).
You should be able to handle this using vanilla JS. The Internationalization API is part of JS core: ECMAScript Internationalization API
https://www.w3.org/International/wiki/JavaScriptInternationalization
This answer worked for me: How to format numbers as currency strings

Using arrays to do basic calculations with negative exponent

I'm trying to write a function which outputs the correct result when multiplying a number by a negative power of ten using arrays and split() method. For example the following expressions get the right result: 1x10^-2 = 0.01 1x10^-4 = 0.0001.
Problem comes when the number's length is superior to the exponent value (note that my code treats num as a string to split it in an array as shown in code bellow :
//var num is treated as a string to be splited inside get_results() function
//exponent is a number
//Try different values for exponent and different lengths for num to reproduce the problem
//for example var num = 1234 and var exponent = 2 will output 1.234 instead of 12.34
var num = '1';
var sign = '-';
var exponent = 2;
var op = 'x10^'+sign+exponent;
var re = get_result(num);
console.log(num+op +' = '+ re);
function get_result(thisNum) {
if (sign == '-') {
var arr = [];
var splitNum = thisNum.split('');
for (var i = 0; i <= exponent-splitNum.length; i++) {
arr.push('0');
}
for (var j = 0; j < splitNum.length; j++) {
arr.push(splitNum[j]);
}
if (exponent > 0) {
arr.splice(1, 0, '.');
}
arr.join('');
}
return arr.join('');
}
Demo here : https://jsfiddle.net/Hal_9100/c7nobmnj/
I tried different approaches to get the right results with different num lengths and exponent values, but nothing I came with worked and I came to the point where I can't think of anything else.
You can see my latest try here : https://jsfiddle.net/Hal_9100/vq1hrru5/
Any idea how I could solve this problem ?
PS: I know most of the rounding errors due to javascript floating point conversion are pretty harmless and can be fixed using toFixed(n) or by using specialized third-party librairies, but my only goal here is to get better at writing pure javascript functions.
I am not sure if you want to keep going with the array approach to a solution, but it seems like this could be solved with using the Math.pow() method that already exists.
function computeExponentExpression ( test ) {
var base;
var multiplier;
var exponent;
test.replace(/^(\d+)(x)(\d+)([^])([-]?\d+)$/, function() {
base = parseInt(arguments[1], 10);
multiplier = parseInt(arguments[3], 10);
exponent = parseInt(arguments[5], 10);
return '';
} );
console.log( base * Math.pow(multiplier, exponent));
}
computeExponentExpression('1x10^-4');
computeExponentExpression('1x10^2');
computeExponentExpression('4x5^3');
The problem is where you push the decimal point .
instead of
arr.splice(1, 0, '.');
try this:
arr.splice(-exponent, 0, '.');
See fiddle: https://jsfiddle.net/free_soul/c7nobmnj/1/

how to round up if value is two decimal and format as 3 decimals in javascript?

Test cases:
var num1 = 10.66;
var num2 = 10.7898
The function I found on stackOverFlow:
function formatUserCurrencyValue(fieldValue){
var num = parseFloat(fieldValue);
var str = num.toFixed(10);
str = str.substring(0, str.length-7);
return str;
};
I would like the result to be like this: if 10.66 then 10.670 and if 10.78998 then 10.789. Basically if the value has 2 decimal places then the result should round up the first and then format as 3 decimals. If more than 2 decimals (eg. 10.78998) then 10.789, trimming out values after 3 decimals.
Could somebody please tell me how I can achieve this? I tried with the above function as well as some others I found but the result is not what I expected for the 10.66 scenario. I am getting 10.660 instead of 10.670.
It looks like a similar question has already been asked here: Formatting a number with exactly two decimals in JavaScript
I liked the answer that #Miguel had. Using a function to do the conversion.
function precise_round(num, decimals) {
var t=Math.pow(10, decimals);
return (Math.round((num * t) + (decimals>0?1:0)*(Math.sign(num) * (10 / Math.pow(100, decimals)))) / t).toFixed(decimals);
}
Then use the function.
precise_round(num,3)
Setting aside the fact that rounding num1 will produce 10.66 and not 10.67 what you want can be achieved with the below function which will print the appropriate results to the console.
var num1 = 10.66;
var num2 = 10.7898;
var roundIt = function(num) {
console.log(parseFloat(Math.round(num * 1000) / 1000).toFixed(3));
};
roundIt(num2); //10.790
roundIt(num1); //10.660
Since using the toFixed() method returns a string we wrap the result in parseFloat() so that we get a floating point number.
Here you have what you ask, it seems weird to me. Round all like this is something "special" at least... but, is exactly what you ask.
var weirdRound = function(n) {
var splited = n.toString().split(".");
var res = 0;
if(splited[1]) {
var len = splited[1].length;
if(len > 3) {
splited[1] = splited[1].substr(0, 3);
res = (splited.join(".") *1).toFixed(3);
} else if(len == 2) {
splited[1] = splited[1].substr(0, 1) + ((splited[1].substr(len -1, len) *1) +1);
res = (splited.join(".") *1).toFixed(3);
} else if(len == 1) {
res = n.toFixed(2)
} else {
res = n.toFixed(3);
}
}
return res.toFixed(3);
}
console.log(weirdRound(10.66));
console.log(weirdRound(10.9));
console.log(weirdRound(10.7898));
console.log(weirdRound(1.12));
console.log(weirdRound(1.12565));
console.log(weirdRound(1.125));

How to sum 2 numbers witout rounding

I have 2 numbers
a = 1548764548675465486;
b = 4535154875433545787;
when I sum these number they are rounded to
a => 1548764548675465500
b => 4535154875433545700
and a + b returns 6083919424109011000 while it should return 6083919424109011273
is there a javascript solution to solve this problem witout the use of a library ?
To work around the precision limitations associated with JavaScript's numbers, you will need to use a BigInteger library like the popular one offered here: http://silentmatt.com/biginteger/
Usage:
var a = BigInteger("1548764548675465486");
var b = BigInteger("4535154875433545787");
var c = a.add(b);
alert(a.toString() + ' + ' + b.toString() + ' = ' + c.toString());
// Alerts "1548764548675465486 + 4535154875433545787 = 6083919424109011273"
Demo: http://jsfiddle.net/69AEg/1/
There are no integers in Javascript, all numbers are double precision floating point.
That gives you a precision of around 15-16 digits, which is what you are seeing.
as per this question
and potential solution i.e. use a library
Personally, I would not use javascript, never been great at numbers. Just try typing 0.1 + 0.2 into any browsers console window. Result is 0.30000000000000004.
Send the calculation to your server side language (as a string) and do the work there, you should have a better outcome.
Technical article on the nuances of floating point numbers here, if you interested
Well, here is a solution I found witout the use of any external library, all I need to do is to define a class that had a property value wich should be a string, and define the function plus
function LongNumber()
{
// it takes the argument and remove first zeros
this.value = arguments[0].toString();
while(this.value[0]==="0")
this.value = this.value.substr(1);
// this function adds the numbers as string to another string and returns result as LongNumber
this.plus = function (Num)
{
var num1 = pad(Num.value.length, this.value);
var num2 = pad(this.value.length, Num.value);
var numIndex = num1.length;
var rest = 0;
var resultString = "";
while (numIndex)
{
var number1 = parseInt(num1[(numIndex)-1]);
var number2 = parseInt(num2[(numIndex--)-1]);
var addition = (number1+number2+rest)%10;
rest = parseInt((number1+number2+rest)/10);
resultString = addition.toString() + resultString;
}
return new LongNumber((rest?rest.toString():"") + resultString);
}
function pad(width, string)
{
return (width <= string.length) ? string : pad(width, '0' + string)
}
}
All i need to do now is to declare 2 LongNombers and use the function plus
var Number1 = new LongNumber("1548764548675465486");
var Number2 = new LongNumber("4535154875433545787");
var Result = Number1.plus(Number2);
Result.value // returns "6083919424109011273"

Is there ay JS function to find value before and after decimal point

I am validating a decimal number using JavaScript.
Am just using NaN
var a = 12345.67
Is there any javascript function to get the count or the value itself before and after decimal point .
before() should return 1234
after() should return 67
Please dont suggest a substring!
var a = 12345.67;
alert(a.toString().split(".")[0]); ///before
alert(a.toString().split(".")[1]); ///after
Here is a simple fiddle http://jsfiddle.net/qWtSc/
zzzzBov's suggestion is this
Number.prototype.before = function () {
var value = parseInt(this.toString().split(".")[0], 10);//before
return value ? value : 0;
}
Number.prototype.after = function () {
var value = parseInt(this.toString().split(".")[1], 10);//after
return value ? value : 0;
}
Usage
alert(a.before()); ///before
alert(a.after()); ///after
before is easy. It's just a round down operation.
var before = function(n) {
return Math.floor(n);
};
after is harder without string processing. I mean how would you handle after(Math.PI)? You can't hold a integer with an infinite number of digits after all.
But with some string processing it's easy, just know it won't be exact due to the wonders of floating point math.
var after = function(n) {
var fraction = n.toString().split('.')[1];
return parseInt(fraction, 10);
};
Playing off of other answers... and you wanted a 'numeric' version.. still easiest to convert it to a string and work off the split function...
function getNatural(num) {
return parseFloat(num.toString().split(".")[0]);
}
function getDecimal(num) {
return parseFloat(num.toString().split(".")[1]);
}
var a = 12345.67;
alert(getNatural(a)); ///before
alert(getDecimal(a)); ///after
http://jsfiddle.net/rlemon/qWtSc/1/
var decimalPlaces = 2;
var num = 12345.673
var roundedDecimal = num.toFixed(decimalPlaces);
var intPart = Math.floor(roundedDecimal);
var fracPart = parseInt((roundedDecimal - intPart), 10);
//or
var fractPart = (roundedDecimal - intPart) * Math.pow(10, decimalPlaces);
To find the count/length of characters after dot:
var a = 12345.67;
var after_dot = (a.toString().split(".")[1]).length;
var before_dot= (a.toString().split(".")[0]).length;
Unfortunately there's no way to get the factional part in a reliable way using math functions, because pretty odd roundings often occur, depending on the Javascript engine used.
The best thing to do is to convert it to a string, and then checking if the results is in decimal or scientific notation.
Number.prototype.after = function() {
var string = this.toString();
var epos = string.indexOf("e");
if (epos === -1) { // Decimal notation
var i = string.indexOf(".");
return i === -1 ? "" : n.substring(i + 1);
}
// Scientific notation
var exp = string.substring(epos + 1) - 0; // this is actually faster
// than parseInt in many browsers
var mantix = n.string.substring(0, epos).replace(".", "");
if (exp >= -1) return mantix.substring(exp + 1);
for (; exp < -1; exp++) mantix = "0" + mantix;
return mantix;
}
If your digits after decimal point are fixed, then this solution works without converting to string.
This example shows a solution for 2 digits after decimal.
Before decimal:
const wholeNum = Math.floor(num);
After decimal:
let decimal = (num - wholeNum) * 100

Categories