Convert European price to another format - javascript

I'm trying to convert several European price formats to other formats. The European format is like this:
€1.795,00
I need to convert it to
€1,795.00
I'm using this script http://josscrowcroft.github.io/money.js/ to convert currencies from one to another. However when when it reads something like €1.795,00, it thinks its €1.79 which is problematic.
Is there a method which would convert any decimals to dots and vice versa? Or is there some other alternative method to achieve what I need.

There might be a cleaner solution but this is the easy one:
str.replace(",",";");
str.replace(".",",");
str.replace(";",".");

str = "€1.795,00";
str = str.replace(",",";").replace(".",",").replace(";",".");
Now, str = "€1,795.00";
str = str.replace(",",";").replace(".",",").replace(";",".");
Now, str = "€1.795,00";

Thanks for the reply everyone, actually I found the solution I was looking for. Here, for anyone else looking for the answer to this.
I'm using money.js as mentioned above to convert currencies and it so happens another script called accounting.js is also necessary if you want to format currencies.
http://josscrowcroft.github.io/accounting.js/
If you have something like €1.700,00, use method accounting.unformat from accounting.js and you'll get this 1700
Then simply convert it with money.js :)
btw, the function explanation is near the bottom of the link

Related

Add o'clock/Uhr to a globalize.js formatted date

Pretty simple question: I want to add a switch to my globalize format skeleton, so that it adds an "o'clock" or German "Uhr" after the time. Right now, I'm using the following pattern:
yMMddHHmm
I found the o'clock string in the respective cldr files, but I don't know how to add them to the skeleton.
Here's my code:
Globalize.formatDate(dTermin, { skeleton: 'yMMddHHmm' });
Thank you in advance for your help!
There's no skeleton that would give you "o'clock" kinda output [1]. Although, if you really want that format and are willing to provide the i18n data for all the locales you think could support it you can use globalize's raw option like:
// Not recommended anyway...
if (locale === "en") {
formatter = globalize.dateFormatter({raw: "HH 'o''clock'"});
}
Though, it's usually a not recommended approach since you have to maintain this list of custom formatters yourself.
1: In order to check, I've grep'ed the whole CLDR and found no match.

AngularJS math joining strings instead of adding

I have a standard $scope.totals = $scope.totals = {storage:0, dailystorage:0}; and an angular.forEach that adds cam.storage to the $scope.totals.storage to give me the total storage.
I am using this to do that:
$scope.totals.storage = $scope.totals.storage+cam.storage;
The problem is that, say if two cam.storage are 21.09 and 15.82, it'll make $scope.totals.storage 21.0915.82 - basically adding them like strings instead of like math.
How do I make it an addition - not a joining?
Judging from what you've posted (verifying that $scope.totals is already a number), cam.storage is a string. You need to parse it to a number before adding it to the existing value:
$scope.totals.storage += parseFloat(cam.storage);
If they are concatenating instead of adding, it sounds like you need to parse them as decimals (You can also use toFixed(int) to limit the decimals as needed).
$scope.totals.storage = parseFloat($scope.totals.storage)+parseFloat(cam.storage);
My solution I use {{(a*1)+(b*1)}} It work.

Parse twitter-cldr-js formatted number back into integer or float?

Is there any easy way to do reverse of:
// include twitter_cldr/es.js
var fmt = new TwitterCldr.DecimalFormatter();
fmt.format(1337); // "1.337"
i.e. parse "1.337" back to integer with value of 1337?
The solution should work for any twitter-cldr-js supported locale.
I am not looking for parseInt / parseFloat, as it does not handle all possible locales.
After two days of trial & error I've found it impossible to do this using twitter-cldr-js and turned to Globalize instead, which does the job very neatly.

is there any way to parse date in string using Datejs?

I came across Datejs recently and found it very useful. However I could not figure out if there is a way to parse a string and extract only date part from it using the same.
For example, if there is a string >> "I will start exercise from next Monday."
Then it should parse the string, extract 'next monday' from it and convert it into date and give me the result.
How can it be implemented?
Thanks :)
You can write some RegEx for that. That would be the easiest way. 'next' will be a keyword in that case. A simple function can lookup the current weekday and return the date of the next monday. Should not that complicated.
Edit:
You can do something like this:
var pattern = /^([\w\W.]*)(next){1}([\sa-zA-Z]*)/;
while (result = pattern.exec(yourTextVariable) != null){
// read the data as you need from the result array
}
The Pattern above expecting a white space then the keyword next and will red the next word if it only has alpha-letters. (please note that the RegEx is untested and may need some refactoring to fit your needs. You may take a look at this page to do this: javascriptkit.com)

Parse ONLY a time string with DateJS

I'm using the excellent (but large) DateJS library to handle dates and times in my webapp. I just came across something that I'm not sure how to handle.
I want my users to be able to enter Time strings only, without a date, but they should be able to enter it in any manner they please. For instance:
5:00 pm
17:00
5:00pm
5:00p
5p
etc.
Using Date.parse(value) converts these strings into a full date, which is exactly what I want. However, it also allows the user to enter any other part of a date string, such as:
sat 5pm
1/1/2010 5pm
etc.
I'm trying to use DateJS to validate an input field for a time value. Something like:
function validateTime(value) {
return Date.parse(value) !== null;
}
Is there a way to use DateJS features to solve this? There are other SO questions that provide solutions, but if DateJS has a way to do this, I don't really want to add more custom code to my app to do this.
Shortly after asking my question, I discovered that Date.parseExact() can take an array of format strings. Somehow I'm missed that. I managed to get something working with the following code:
function validateTime(input) {
return Date.parseExact(input, [
"H:m",
"h:mt",
"h:m t",
"ht","h t"]) != null ||
Date.parseExact(input, [
"h:mtt",
"h:m tt",
"htt","h tt"]) != null;
};
Note that some formats don't seem to be able to be included together at the same time, which is why I split them into two separate parseExact() calls. In this case, I couldn't include any string that contained a single t in it with format strings that contained a double tt in it.
The additive approach seems cumbersome. Takes away the beauty of DateJS in my opinion. I needed the same solution and decided to just sneakily append the date in front of my input string before parsing with DateJS:
var parsed = Date.parse(Date.today().toString('M/d/yyyy') + ' ' + this.value);
if (parsed) {
alert(parsed.toString('h:mm tt'));
}
Now DateJS will not be sniffing around for any of its date-part parsing patterns, as you have already subbed it in.
Hope this helps someone!

Categories