How to identify and format numbers with javascript - javascript

My problem is occurs for some random reason most of the graphics library do not use the internalization of currencies so the values I am wanting to read and transfer to the graphs are already formatted using my local currency already tried in many ways to solve this and not I managed, however, to arrive at a possible and most probable solution it works this way:
1,00 | 1
10,00 | 10
500,00 | 500
5.000,00 | 5000
70.000,00 | 70000
900.000,00 | 900000
1.000.000,00 | 1000000
Especially when I set these values in strings I wanted to remove all semicolons and last two numbers in values below why if I just remove the commas the graph will only read as a value of 100 or 1000 for example:
1,00 => 1
10,00 => 10
And so on to any number that comes after comma must be removed and at the beginning only the points be removed...
A brief demonstration of how the result is on my chart:
http://jsfiddle.net/Qa2Tx/29/
If you know some way to format numbers of this work without touching the commas and dots of my local currency I will explode with joy but if you can only format as I mentioned above I will be extremely happy and thank you for two days I am trying to solve this :D

I made a small demo of what you can do. You can use the split() function to split on the ,. This will divide the value on to sides
10,00 => ['10','00']
And you can just target the first part afte the split
This is a demo for just one value
var value = '1.000,00';
console.log(value.split(',')[0]);
This demo also remove the . and turns the value to a number
var value = '1.000,00';
console.log(Number(value.split(',')[0].replace(/[^0-9]+/g,"")));
This demo represent an array of values
var arr = ['1,00',
'10,00',
'500,00',
'5.000,00',
'70.000,00',
'900.000,00',
'1.000.000,00']
arr.map(value => console.log(Number(value.split(',')[0].replace(/[^0-9]+/g,""))))
Hope this helps :)

Related

(solved) Javascript : Cutting/Removing excess characters from a string if it exceeds the Limit

This question is a bit tricky to explain. Say i have an variable whose length is dynamic meaning it is random,i want to change its length to the first 5 or a certain amount of characters regardless of the length of the characters existing in the variable. I hope i could explain what i am trying to do
.________________________________________________________________________.
I really dont know which direction to go in or what step to take in order to reach my goal but i just copy/pasted some random code of the internet that didn't work so i did not think it is of any importance to include in this query,but i could share on demand
Pseudo code...
const str = "the string";
const start = 0;
// const end = str.length;
str.substring(start, (optional) end);
So if you want just first five characters, you do something like
str.substring(0, 5);
If it's only integers (numbers) you could simply generate a random whole number between 11111 and 99999 (so it's always 5 digits), like this:
let x = Math.floor((Math.random() * 99999) + 11111);
do you have a delimited amount of decimals? if not, you should remove Math.floor and convert into string and after that substring as in the other answer (and convert back into a Number if needed as Number). If yes, you should change the "11111" and "99999" to the length of integers needed, and use toFixed() to limit the number of decimals. –
DavidTaubmann

javascript output random line from input

Is there a way to chose a random line from an input or textarea with a javascript?
Currently I find a few results that seems to be working, but mostly use jquery and other additional languages. I'm looking for Javascript only.
I've seen javascripts that pick a random line from a local file, I know how that works, but is there a way to do it without local file, and just from an input?
Thanks for your help!
Firstly, split your input text by newline characters (that will produce an array of lines). Then take a random element from that array. To achieve this, use Math.random() function which produces a random float between 0 and 1 and then multiply it by lines array length to get a random float between 0 and lines.length. And finally, use Math.floor() to get an integer to be used as an index.
const lines = input.split('\n');
const randomLine = lines[Math.floor(Math.random() * lines.length))];

Remove currency symbol from string and convert to a number using a single line in Javascript

I have a string below that is a price in £, I want to remove the currency symbol and then convert this into a number/price I can use to compare against another value (eg. X >= Y ...)
£14.50
I have previously converted strings to numbers used for currency with
var priceNum = parseFloat(price);
IDEAL OUTCOME
14.50 as a number value. Can this be done in a single line?
I found this very helpful
var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9\.-]+/g,""));
Convert (Currency) String to Float
If the currency symbol will always be there, just use substring:
var priceNum = parseFloat(price.substring(1));
If it may or may not be there, you could use replace to remove it:
var priceNum = parseFloat(price.replace(/£/g, ""));
Beware that parseFloat("") is 0. If you don't want 0 for an empty input string, you'll need to handle that. This answer has a rundown of the various way to convert strings to numbers in JavaScript and what they do in various situations.
Side note: Using JavaScript's standard numbers for currency information is generally not best practice, because if things like the classic 0.1 + 0.2 issue (the result is 0.30000000000000004, not 0.3). There are various libraries to help, and BigInt is coming to JavaScript as well (it's a Stage 3 proposal at the moment, currently shipping in Chrome). BigInt is useful because you can use multiples of your basic currency (for instance, * 100 for pounds and pence).
try this number-formatter-npm library. This library is fantastic.
npm i number-formatter-npm
documentation:https://www.npmjs.com/package/number-formatter-npm

HTML: Adding commas and limited decimal places of a number

I've figured out in the past that if I want a number 100000 to have commas then I just add .toLocaleString() to the angularJS variable. ie:
<span>{{variable.toLocaleString()}}</span>
Also, if want to limit the number of decimal places to a number 20.34343434 to lets say 2, then I would just add .toFixed(2). ie:
<span>{{variable.toFixed(2)}}</span>
Now I would like to do both. ie:
<span>{{variable.toFixed(2).toLocaleString()}}</span>
or
<span>{{variable.toLocaleString().toFixed(2)}}</span>
but neither seem to work. Maybe the solution to this has nothing to do with either function.
Question: How do I add commas and limit the amount of decimal places in a value using HTML and angularJS?
You can do this with a currency filter using an empty string for the currency symbol:
{{variable | currency:""}}
If you want to change the number of decimal places, you can specify it as an argument (default is 2):
{{variable | currency:"":3}}
If you insist on using function then you can use:
{{variable.toLocaleString("nu", {minimumFractionDigits: 2, maximumFractionDigits: 2})}}
This will take care of both commas/spaces (depending on locale) in integer digits as well as separator (. or ,) depending on locale.

Building an effective js/ jQ 'thousands' comma separator that works for large numbers (10^6+) on key up

I have spent time looking through each of the proposed solutions to the problem of adjusting user input strings as they type to format a large number to have comma separators:
9999999999 --> 9,999,999,999 and so on.
There are several discussions here on stack overflow:
How to print a number with commas as thousands separators in JavaScript
Adding comma as thousands separator (javascript) - output being deleted instead
and many more related queries, I've tested those that I could find, and they worked well for numbers up to a power of 4 or 5, eg 12,345 comes out nicely,
But larger numbers end up with far too many commas:
1,,,,,0,,,0,,,000
for example is the output of the code I currently have implemented. (It also registers backspace as a key stroke, so when I backspace three times it adds a comma...)
function numberWithCommas(n){
var s=n.split('.')[1];
(s) ? s="."+s : s="";
n=n.split('.')[0]
while(n.length>3){
s=","+n.substr(n.length-3,3)+s;
n=n.substr(0,n.length-3)
}
return n+s
}
and then on the input:
onkeyup="this.value=numberWithCommas(this.value);"
Am I using the function incorrectly? Or is it simply not viable for larger numbers. Is there a way it can be adjusted to work for any size of number?
Thanks in advance
You are just adding comas to a value which already as commas after the first iteration... You should just reset the commas at the beginning of your function numberWithCommas:
n = n.replace(/,/g, "");

Categories