Convert string to number using i18n in AngularJS - angularjs

Using the ng.IFilterService in AngularJs I can convert a number to a string. The numberFilter uses i18n to determine if it needs a period or a comma to format the number.
Like so:
var numberString = $filter('number')(1.1234, 2);
// numberString = 1,12 when using i18n for nl-nl (Netherlands)
What I am looking for, and so far haven't found, is how to do this in reverse.
When having a string value '1,12' and knowing the i18n setting for decimal separator (in this case a comma) one would know enough to convert it back. Although I can't find any method in AngularJS to do this.

Related

How to get raw date string from date picker?

I'm struggling for hours with this seemingly trivial issue.
I have a antd datepicker on my page.
Whenever I choose a date, instead of giving me the date I chose, it gives me a messy moment object, which I can't figure out how to read.
All I want is that when I choose "2020-01-18", it should give me precisely this string that the user chose, regardless of timezone, preferably in ISO format.
This is not a multi-national website. I just need a plain vanilla date so I can send it to the server, store in db, whatever.
Here are some of my trials, so far no luck:
var fltval = e;
if (isMoment(fltval)) {
var dat = fltval.toDate();
//dat.setUTCHours(0)
fltval = dat.toISOString(); // fltval.toISOString(false)
var a = dat.toUTCString();
//var b = dat.toLocaleString()
}
It keeps on moving with a few hours, probably to compensate for some timezone bias
UPDATE 1:
the datestring is data-wise correct. But its not ISO, so I cant use it correctly. I might try to parse this, but I cannot find a way to parse a string to date with a specific format.
UPDATE 2:
I also tried adding the bias manually, but for some reason the bias is 0
var dat = pickerval.toDate()
var bias = Date.prototype.getTimezoneOffset()// this is 0...
var bias2 = dat.getTimezoneOffset()// and this too is 0
var d2 = new Date(dat.getTime()+bias)
var mystring= dat.toISOString() //still wrong
Thanks!
Javascript date functions can be used,
I assume you are getting in 2022-01-03T11:19:07.946Z format then
date.toISOString().slice(0, 10)
to 2022-01-03
There are 2 ways to get the date string:
Use the moment.format api:
date.format("yyyy-MM-DD")
Use the date string that is passed to the onChange as second parameter
Here is a Link.
I am assuming your code snippet is inside the onChange method. This gives you a moment and a date string to work with (the first and second parameters of the function respectively).
You have a few options. You could set the format prop on the DatePicker to match the format of the string you want. Then just use the date string. Or you can use the moment object as Domino987 described.

Jhipster dataUtils.downloadFile errors out for content with non ASCII characters

I have been trying to use the inbuilt dataUtils.downloadFile function from JHipster on the angular side. This accepts a content string and content type and let the user download the content as a file.
I noticed that, it can easily process content which contains ASCII character. However, it fails to process UTF-8 character set.
This is the error I get :
Failed to execute 'atob' on 'Window'
Am I missing something or is there a way to get around with it?
Currently i have to go through my file to replace all UTF-8 only chars to ASCII but that would be tedious.
Thanks for reading..
EDIT:
Below is the field definition.
{
"fieldName": "troubleshooting",
"fieldType": "byte[]",
"fieldTypeBlobContent": "text"
}
Here is the angular code which tries to convert the string to base64 and then download.
The problem is not with base 64 encoding. It is fine. The problem is with content format. If the content contains UTF-8 only chars, then it fails. In other cases I get the file downloaded successfully
download(appliance: Appliance) {
const applianceObj = JSON.parse(appliance.troubleShooting);
const prettyPrinted = JSON.stringify(applianceObj, null, 2);
const data = this.base64Utils.encode(prettyPrinted);
this.dataUtils.downloadFile('application/json', data, appliance.applianceType);
}

how to write script on validating number from the text? for ex: i like to get numeric value "2" from field "Staus(2)"

On Selenium, I'm writing script to get the number from the text. suppose there is a field 'Status(2)'. The number in the brackets keep changing. I want to get the value.
This code should get back the text for the element you have provided:
WebElement web_element_found = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects"));
String element_text = web_element_found.getText();
Then you can have a look at this answer for how to use regex to extract the digit from the string: Regex to extract digit from string in Java
Hope this helps!
Here is the solution.
String rawText = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects")).getText();
String number = rawText.substring(s.indexOf("(") + 1).substring(0, s.indexOf(")"));
System.out.println(number);

Overriding Ext.util.Format.thousandSeparator except when formatting money

I am trying to override Ext.util.Format.decimalSeparator and thousandSeparator. So, in my app, when I chnage the language to Spanish and I try using this function, Ext.util.Format.number(1234,'$0,000'), still it converts the number to 1.234 instead of 1,234.
I want that, irrespective of what language I choose, it should always format the money to $0,000 format and not using my selected locale, e.g., never $0.000. I observed if I change the thousandSeparator of Ext.util.Format object, it works fine. So, I added the following code in Ext.Loader.loadScript callback function in launch function in Application.js,
var utilFormatObj={};
utilFormatObj.thousandSeparator = ",";
utilFormatObj.decimalSeparator = ".";
Ext.override(Ext.util.Format, utilFormatObj);
BUt, it seems to work only in this place, once it loads the app on webpage, it again gets back to thousandSeparator=".". I can see that ext-lang-es.js file has the function which sets these properties. Can anyone suggest how can I catch whether the app is completely loaded on webapge and then use the above code there. Thank you.
When you call Ext.util.Format.number() you're not specifying what to use as decimal or thousand separator, you're only specifying precision, whether to show thousands separator, and whether to pad precision with zeroes.
The documentation for Ext.util.Format.number states:
The format string must specify separator characters according to US/UK conventions ("," as the thousand separator, and "." as the decimal separator)
Therefore if you want to display numbers in different locales, you have to run the code that changes the default separators before calling Ext.util.Format.number or Ext.util.Format.currency.
var value = 202020.20, format = "0,000.0000";
// Print in Spanish
Ext.util.Format.thousandSeparator = ".";
Ext.util.Format.decimalSeparator = ",";
alert(Ext.util.Format.number(value, format));
// Print in Swedish French
Ext.util.Format.thousandSeparator = "'";
Ext.util.Format.decimalSeparator = ",";
alert(Ext.util.Format.number(value, format));
// Print in English
Ext.util.Format.thousandSeparator = ",";
Ext.util.Format.decimalSeparator = ".";
alert(Ext.util.Format.number(value, format));
Here's a hack you can use if you really want to specify that currency should always use a period as the thousand separator but still have Ext.util.Format.number use the selected locale's separators.
function formatMoney(amount, sign, decimals, end) {
// Save the thousand separator
var thousandSep = Ext.util.Format.thousandSeparator;
Ext.util.Format.thousandSeparator = '.';
var formatted = Ext.util.Format.currency(amount, sign, decimals, end);
// restore the thousand separator
Ext.util.Format.thousandSeparator = thousandSep;
return formatted;
}
Example for the above code snippets: https://fiddle.sencha.com/#fiddle/9vm
I am guessing that you are not using the loader after you build your application for deployment. Typically the dynamic loader is only used for development (so you can see each script individually) and you use a faster method in prod.
You could load your Ext overrides on the callback for Ext.define:
Ext.define( className, data, [createdFn] )
where createdFn is a function that contains your Ext overrides. This approach may lend itself to race conditions if you invoke that Format object before the override is applied. To be confident, you could add another JS file with your overrides (after Ext is loaded, before your app code) and make sure that is included when you load your app.

Why do the ngModelCtrl.$formatters get run from last to first

The ngModelCtrl.$parsers gets run through from the first parser in the array to the last parser in the array, while ngModelCtrl.$formatters gets run through from the last formatter in the array to the first formatter in the array. Just wondering the rationale behind this.
Snippet from angular.js (v1.2.1):
...
var formatters = ctrl.$formatters,
idx = formatters.length;
ctrl.$modelValue = value;
while(idx--) {
value = formatters[idx](value);
}
...
That allows to always push the parsers and the formatters.
Let's say you have a parser that transforms the entered string into a number of milliseconds, and then another parse that transforms the milliseconds into a Date.
You'll need corresponding formatters: one that transforms a Date into a number of milliseconds, and a second one that transforms the milliseconds into a String. Having the formatters run in the inverse order of the parsers makes sense: you can simply do
ctrl.$parsers.push(stringToMillis);
ctrl.$formatters.push(millisToString);
...
ctrl.$parsers.push(millisToDate);
ctrl.$formatters.push(dateToMillis);

Resources