Localized currency input that allows for different currencies and regions - html

In my web app I need to allow the user to enter in money into an input field. The currency and how the amount is formatted can be different in different fields. So while the user can enter this in one field:
$1,234.56
for US dollars for the United States region, they can enter this in another:
€ 1.234,56
for euros using Germany as the region.
Regardless what the currency is and what region the amount is formatted in, the value internally should be returned in the same manner, which in this case would be:
1234.56
I could just use an input field with a type set to "text" but then I would have to write code to check for the currency symbol and how the value is formatted. But it's possible that the user doesn't enter a currency symbol. Also, I don't see a clear way of distinguishing regional numbers without the currency symbol.
Is there any solution that lets the user select the currency symbol and manages the value entered to provide a consistent internal value?

If there is a way to achieve this using just a specifically typed HTML input I don't know about it, the closest one would be type=number, though that doesn't allow for formatted numbers.
For a text type and having code check the input, if you can assume the numbers to always be formatted as latin numbers, you can use a rather naive approach using regular expressions.
function normalize(currency) {
const naive = /(?:(-)\s*)?(?:[^0-9]+\s*)?([0-9]{1,3}(?:[ ,\.]?[0-9]{3})*)(?:[,\.]([0-9]+))?(?:\s*[^0-9-]+)?(?:\s*(-))?/;
const [,mina,base,fraction,minb] = currency.match(naive)||[];
const min = mina || minb ? '-' : '';
return base ? Number(`${min}${base.replace(/[^\d]+/g, '')}.${fraction||0}`) : '';
}
const output = document.querySelector('output');
document.querySelector('input[name=currency]')
.addEventListener('input', (event) => {
const { value } = event.target;
output.value = normalize(value);
});
<label>
amount <input type=text name=currency>
</label>
<div>
The normalized value: <output></output>
</div>
The expression is build around the following concepts:
(?:(-)\s*)?(?:[^0-9]+\s*)?: a minus symbol may be before the number, optionally followed by a currency symbol
([0-9]{1,3}(?:[ ,\.]?[0-9]{3})*): a base number which may contain grouping symbols and groups of three numbers
(?:[,\.]([0-9]+))?: an optional fraction number, indicated by either a comma or period
(?:\s*[^0-9-]+)?(?:\s*(-))?: a minus symbol may be after the number, optionally preceded by a currency symbol
This works for a lot of use cases, though I consider it naive mostly because it does not verify the "thousands separator" not being used also as fraction separator.
a more thorough example

Related

NetSuite Saved Search: REGEXP_SUBSTR Pattern troubles

I am trying to break down a string that looks like this:
|5~13~3.750~159.75~66.563~P20~~~~Bundle A~~|
Here is a second example for reference:
|106~10~0~120~1060.000~~~~~~~|
Here is a third example of a static sized item:
|3~~~~~~~~~~~5:12|
Example 4:
|3~23~5~281~70.250~upper r~~~~~~|
|8~22~6~270~180.000~center~~~~~~|
|16~22~1~265~353.333~center~~~~~~|
Sometimes there are multiple lines in the same string.
I am not super familiar with setting up patterns for regexp_substr and would love some assistance with this!
The string will always have '|' at the beginning and end and 11 '~'s used to separate the numeric/text values which I am hoping to obtain. Also some of the numeric characters have decimals while others do not. If it helps the values are separated like so:
|Quantity~ Feet~ Inch~ Unit inches~ Total feet~ Piece mark~ Punch Pattern~ Notch~ Punch~ Bundling~ Radius~ Pitch|
As you can see, if there isn't something specified it shows as blank, but it may have them in another string, its rare for all of the values to have data.
For this specific case I believe regexp_substr will be my best option but if someone has another suggestion I'd be happy to give it a shot!
This is the formula(Text) I was able to come up with so far:
REGEXP_SUBSTR({custbody_msm_cut_list},'[[:alnum:]. ]+|$',1,1)
This allows me to pull all the matches held in the strings, but if some fields are excluded it makes presenting the correct data difficult.
TRIM(REGEXP_SUBSTR({custbody_msm_cut_list}, '^\|(([^~]*)~){1}',1,1,'i',2))
From the start of the string, match the pipe character |, then match anything except a tilde ~, then match the tilde. Repeat N times {1}. Return the last of these repeats.
You can control how many tildes are processed by the integer in the braces {1}
EG:
TRIM(REGEXP_SUBSTR('|Quantity~ Feet~ Inch~ Unit inches~ Total feet~ Piece mark~ Punch Pattern~ Notch~ Punch~ Bundling~ Radius~ Pitch|', '^\|(([^~]*)~){1}',1,1,'i',2))
returns "Quantity"
TRIM(REGEXP_SUBSTR('|Quantity~ Feet~ Inch~~~ Piece mark~ Punch Pattern~ Notch~ Punch~ Bundling~ Radius~ Pitch|', '^\|(([^~]*)~){7}',1,1,'i',2))
returns "Punch Pattern"
The final value Pitch is a slightly special case as it is not followed by a tilde:
TRIM(REGEXP_SUBSTR('|~~~~~~~~~~ Radius~ Pitch|', '^\|(([^~]*)~){11}([^\|]*)',1,1,'i',3))
Adapted and improved from https://stackoverflow.com/a/70264782/7885772

Formatting String Value as Monetary Value in Google Apps Script

I have created a Google Form that has a field where a numeric value is entered by the user (with numeric validation on the form) and is then submitted. When the number (e.g., 34.00) gets submitted, it appears as 34 in the Google spreadsheet, which is annoying but understandable. I already have a script that runs when the form is submitted to generate a nicely-formatted version of the information that was submitted on the form, but I'm having trouble formatting that value as a monetary value (i.e., 34 --> $34.00) using the Utilities.formatString function. Can anyone help? Thanks in advance.
The values property of a form submission event as documented in Event Objects is an array of Strings, like this:
['2015/05/04 15:00', 'amin#example.com', 'Bob', '27', 'Bill', '28', 'Susan', '25']
As a result, a script that wishes to use any of these values as anything but a String will need to do explicit type conversion, or coerce the string to number using the unary operator (+).
var numericSalary = +e.values[9];
Alternatively, you could take advantage of the built-in type determination of Sheets, by reading the submitted value from the range property also included in the event. Just as it does when you type in values at the keyboard, Sheets does its best to interpret the form values - in this case, the value in column J will have been interpreted as a Number, so you could get it like this:
var numericSalary = e.range.getValues()[9];
That will be slower than using the values array, and it will still provide an unformatted value.
Formatting
Utilities.formatString uses "sprintf-like" formatting values. If you search the interwebs, you'll find lots of references for sprint variables, some of which are helpful. Here's a format that will turn a floating-point number into a dollar-formatted string:
'$%.2f'
$ - nothing magic, just a dollar sign
% - magic begins here, the start of a format
.2 - defines a number with two decimal places, but unspecified digits before the radix
f - expect a floating point number
So this is your simplest line of code that will do the conversion you're looking for:
var currentSalary = Utilities.formatString( '$%.2f', +e.values[9] );
The correct format to get your string into a proper numeric format is as follows:
var myString = myString.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
If you only care about the $ sign and don't need commas the code below (also shown in one of the answers above will suffice.
var myString = Utilities.formatString( '$%.2f', myString );
In my experience toLocaleString performs sometimes performs strangely in Apps Script as opposed to JavaScript
I know this was years ago but might help someone else.
You should be able to add the commas in between with this
.toLocaleString(); after you format the string decimal then add the '$' sign in the beginning by just concating them together.
Ex:
myString = Utilities.formatString( '%.2f', myNumber );
myString = '$' + myString.toLocaleString();

ParseFloat html

I am a college student just learning HTML. The assignment that I have to complete is a simple grade calculator. There is a single button that calculates everything, and the values go to three empty text fields(total, average, and percentage). The problem that I am having is the total is calculated and shows up in the field, but the number is followed by [object]. The average field shows NaN and the percentage field remains empty. Here is my code.
<input type="button" value="Click to calculate"
onclick="fullmark = parseFloat(document.getElementById('fullBox').value);
science = parseFloat(document.getElementById ('scienceBox').value);
math = parseFloat(document.getElementById('mathBox').value);
computer = parseFloat(document.getElementById('computerBox').value);
english = parseFloat(document.getElementById('englishBox').value);
History = parseFloat(document.getElementById('historyBox').value);
total=science+math+computer+english+history;
average=(total/5);
percentage=(total/fullmark)*100;
document.getElementById('totalBox').value=total;
document.getElementById('averageBox').value=average;
document.getElementById('percentageBox').value=percentage;">
Since you wrote History as the variable name first, but then used history when computing/assigning the value for total, that is a different variable – identifiers in JavaScript are case sensitive.
The only reason you don’t get your script aborted with an error about an undefined variable at that point, is that window.history exists – it’s the object that keeps the current navigation history of the window. (All global variables are properties of the window object, so that’s what your use of history gets resolved to.) And since it’s an object, [object] is what you get as result when transferring it into a string context (which happens through the use of +, which is both the addition and the string concatenation operator).
Apart from that, writing all that code into an onclick handler is kinda terrible style. Go learn how to use proper functions for stuff like this next ;-)

how do i decode/encode the url parameters for the new google maps?

Im trying to figure out how to extract the lat/long of the start/end in a google maps directions link that looks like this:
https://www.google.com/maps/preview#!data=!1m4!1m3!1d189334!2d-96.03687!3d36.1250439!4m21!3m20!1m4!3m2!3d36.0748342!4d-95.8040972!6e2!1m5!1s1331-1399+E+14th+St%2C+Tulsa%2C+OK+74120!2s0x87b6ec9a1679f9e5%3A0x6e70df70feebbb5e!3m2!3d36.1424613!4d-95.9736986!3m8!1m3!1d189334!2d-96.03687!3d36.1250439!3m2!1i1366!2i705!4f13.1&fid=0
Im guessing the "!" is a separator between variables followed by XY where x is a number and y is a lower case letter, but can not quite figure out how to reliably extract the coordinates as the number/order of variables changes as well as their XY prefixes.
ideas?
thanks
Well, this is old, but hey. I've been working on this a bit myself, so here's what I've figured out:
The data is an encoded javascript array, so the trick when trying to generate your own data string is to ensure that your formatting keeps the structure of the array intact. To do this, let's look at what each step represents.
As you're correctly figured out, each exclamation point defines the start of a value definition. The first character, an int value, is an inner count, and (I believe) acts as an identifier, although I'm not 100% certain on this. It seems to be pretty flexible in terms of what you can have here, as long as it's an int. The second character, however, is much more important. It defines the data type of the value. I don't know if I've found all the data types yet, but the ones I have figured out are:
m: matrix
f: float
d: double
i: integer
b: boolean
e: enum (as integer)
s: string
u: unsigned int
x: hexdecimal value?
the remaining characters actually hold the value itself, so a string will just hold the string, a boolean will be '1' or '0', and so on. However, there's an important gotcha: the matrix data type.
The value of the matrix will be an integer. This is the length of the matrix, measured in the number of values. That is, for a matrix !1mx, the next x value definitions will belong to the matrix. This includes nested matrix definitions, so a matrix of form [[1,2]] would look like !1m3!1m2!1i1!2i2 (outer matrix has three children, inner matrix has 2). this also means that, in order to remove a value from the list, you must also check it for matrix ancestors and, if they exist, update their values to reflect the now missing member.
The x data type is another anomaly. I'm going to guess it's hexdecimal encoded for most purposes, but in my particular situation (making a call for attribution info), they appear to also use the x data type to store lat/long information, and this is NOT encoded in hex, but is an unsigned long with the value set as
value = coordinate<0 ? (430+coordinate)*1e7 : coordinate*1e7
An example (pulled directly from google maps) of the x data type being used in this way:
https://www.google.com/maps/vt?pb=!1m8!4m7!2u7!5m2!1x405712614!2x3250870890!6m2!1x485303036!2x3461808386!2m1!1e0!2m20!1e2!2spsm!4m2!1sgid!2sznfCVopRY49wPV6IT72Cvw!4m2!1ssp!2s1!8m11!13m9!2sa!15b1!18m5!2b1!3b0!4b1!5b0!6b0!19b1!19u12!3m1!5e1105!4e5!18m1!1b1
For the context of the question asked, it's important to note that there are no reliable identifiers in the structure. Google reads the values in a specific order, so always keep in mind when building your own encoded data that order matters; you'll need to do some research/testing to determine that order. As for reading, your best hope is to rebuild the matrix structure, then scan it for something that looks like lat/long values (i.e. a matrix containing exactly two children of type double (or x?))
Looks like the developer tools from current browsers (I am using Chrome for that) can give you a lot of info.
Try the following:
Go to Google Maps with Chrome (or adapt the instructions for other browser);
Open Developer Tools (Ctrl + Shift + I);
Go to Network tab. Clear the current displayed values;
Drag the map until some url with encoded data appears;
Click on that url, and then go to the Preview sub-tab;
Try this.
function URLtoLatLng(url) {
this.lat = url.replace(/^.+!3d(.+)!4d.+$/, '$1');
this.lng = url.replace(/^.+!4d(.+)!6e.+$/, '$1');
return this;
}
var url = new URLtoLatLng('https://www.google.com/maps/preview#!data=!1m4!1m3!1d189334!2d-96.03687!3d36.1250439!4m21!3m20!1m4!3m2!3d36.0748342!4d-95.8040972!6e2!1m5!1s1331-1399+E+14th+St%2C+Tulsa%2C+OK+74120!2s0x87b6ec9a1679f9e5%3A0x6e70df70feebbb5e!3m2!3d36.1424613!4d-95.9736986!3m8!1m3!1d189334!2d-96.03687!3d36.1250439!3m2!1i1366!2i705!4f13.1&fid=0');
console.log(url.lat + ' ' + url.lng);

ABAP WebAS Active Codepage

I need to concatenate different lines in a string.
To do so, I need to use CR + LF hexadecimal characters.
The problem is that, when I'm using an 8 bit/char environment, I just need to do something like this:
constants : c_lf type x value '10'.
constants : c_cr type x value '13'.
data : g_html type string.
concatenate '<html>' c_cr c_lf into g_html.
but, when I'm in a 16 bit/char environment, the X variable does not represent the correct hexadecimal representation for CR and LF.
So, I should use something like this:
constants : c_lf(2) type x value '0010'.
constants : c_cr(2) type x value '0013'.
data : g_html type string.
concatenate '<html>' c_cr c_lf into g_html.
So, there is any way to find out the amount of bytes/char in use by ABAP WebAS?
Thanks!
The function TR_GET_IS_UNICODE_SYSTEM indicate if the system is using unicode or not.
It calls the CL_ABAP_CHAR_UTILITIES class to get the CHARSIZE attribute (bite/char) (by the way, this class contains a CR_LF public attribute...)
Regards
Guillaume