javascript now to create two ways currency format? [duplicate] - javascript

This question already has answers here:
How to convert a currency string to a double with Javascript?
(23 answers)
Closed 8 years ago.
I have found this link have a simple and useable funciton to convert number to currency.
www.mredkj.com/javascript/nfbasic.html
but how to turn it back form currency to number...
I already try this :
[http://jsfiddle.net/vb2cb1tm/3/]...
But its failed...
I try to read a formatted string using javascript and make a counting on this valua...

I think something as simple as
function numbers(nStr){
return Number(nStr.replace(/[\$\,]/g,''));
}
might do the trick, though it's hard to tell without seeing your original code.

function cur2num(cur) {
return parseFloat(cur.replace(/,/g,"");
}
if currency signs try /[^0-9]\.\-]/g as first argument to the replace

Related

Convert GUID to 8-4-4-4-12 format in javascript [duplicate]

This question already has answers here:
Javascript string to Guid
(5 answers)
Closed 9 months ago.
I am looking to convert a GUID such as 18d874986a114520bd29466a446a50eb to the format 18d87498-6a11-4520-bd29-466a446a50eb
Is there a function in Javascript for doing this?
I googled a bit and looks like the 8-4-4-4-12 format is called UUID. I however, do not find any to UUID conversion libraries.
Do I need to write my own?
One simple approach uses a regex replacement:
var uuid = "18d874986a114520bd29466a446a50eb";
uuid = uuid.replace(/^([a-f0-9]{8})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{12})$/i, "$1-$2-$3-$4-$5");
console.log(uuid);

Regex after first sign [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
I'm gonna to write regex or other expression to get coordinates after '='.
My example:
var cords = https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac=
I want to get 50.082961,19.966860
I know that I could use slice but I think I could write it better with regex.
Simple base for this example: \=(.[0-9]) What's next?
Try this center\=(\d+\.\d+,\d+\.\d+)&
var val = 'https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac='.match(/center\=(\d+\.\d+,\d+\.\d+)&/)[1]
console.log(val)
But as other's have commented, you likely shouldn't be using regex for this purpose

SyntaxError: Number.toString() in Javascript [duplicate]

This question already has answers here:
Calling member function of number literal
(3 answers)
Closed 6 years ago.
Why do I get an error on this, in Javascript:
10000000.toString();
You can see an example in here:
http://jsbin.com/kagijayecu/1/edit?js,console
It's because the JS parser is expecting more digits after the "." rather than a method name, e.g. 1000000.0, and in fact 1000000.0.toString() will work as expected.
wrap it inside () like this (10000000).toString()
When JS parse meets dot after digit it expects floating-point literal, e.g. 1000000.0

Number format with regex javascript [duplicate]

This question already has answers here:
Javascript Thousand Separator / string format [duplicate]
(15 answers)
Closed 8 years ago.
I have a number is 1205000000, I want display at 1.205.000.000
number.toString().replace(/(\d{3})/g, "$1.").toString()
but result is 120.500.000.0
I don't want reverse a number.
For the sake of correcting your regular expression (obviously for integer values only):
number.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1.');
ยป Detailed Regex Explanation
One way would be to reverse the string before your manipulation and ther reverse it again. Like so:
var number = 1205000000;
function reverse(s) {
return s.split("").reverse().join("");
}
var str = reverse(reverse(number.toString()).replace(/(\d{3})/g, "$1."));
alert(str);
See this working fiddle.
EDIT:
See the comments. Its a bit dirty but for that specific number it will work. The link posted by #Artyom Neustroev as a comment under you question seems a whole lot better than this here.

Replace 56666.666666666664% percentage in to three digit percentage using javascript [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 9 years ago.
I have calculated some data usign javascript and result is printing like this (56666.666666666664%). and I need to convert the result in like 56.66%. Below is a Javascript which print the above result.
$scope.totaTaken = (totalT / data.length)*100;
If you're looking for a JavaScript solution (where you do it in a controller or directive), the given comments answer your question (see toFixed). If you're looking for an AngularJS solution, where you format the value in the view, check out the number filter which was made for this!

Categories

Resources