Cast String to Double using Postgresql - string

How can I cast a string containing double value to numeric value, keeping precision. If the string is not a double, then convert to zero

This query uses a regex match to return a double if the input string has "numeric content", also only digits or decimal numbers with a point as separator like 1.0, 18789.903. Returns 0 if the input string contains non numerics characters like 10hghj. The minus character in front of the string is allowed (negative double):
-- returns 1.2143
WITH subquery AS (SELECT '1.2143'::text as value)
SELECT
CASE WHEN value ~ '^(-)?[0-9]+\.?([0-9]+)?$'
THEN value::double precision
ELSE 0
END FROM subquery;
-- returns 0 because input is a contains not digits characters
-- (the regex expression is the same)
WITH subquery AS (SELECT '1214gh'::text as value)
SELECT
CASE WHEN value ~ '^(-)?[0-9]+\.?([0-9]+)?$'
THEN value::double precision
ELSE 0
END FROM subquery;
Explanation:
The ~ is the regex match operator in PostgreSql. More info here: http://www.postgresql.org/docs/9.4/static/functions-matching.html#FUNCTIONS-POSIX-TABLE
The CASE WHEN THEN ELSE expression allows you to change returned value depending on one or more conditions. In the WHEN part are all statements allowed which return a boolean value. More info: http://www.postgresql.org/docs/9.4/static/functions-conditional.html

Related

Compare Unicode code point range in Python3

I would like to check if a character is in a certain Unicode range or not, but seems I cannot get the expected answer.
char = "?" # the unicode value is 0xff1f
print(hex(ord(char)))
if hex(ord(char)) in range(0xff01, 0xff60):
print("in range")
else:
print("not in range")
It should print: "in range", but the results show: "not in range". What have I done wrong?
hex() returns a string. To compare integers you should simply use ord:
if ord(char) in range(0xff01, 0xff60):
You could've also written:
if 0xff01 <= ord(char) < 0xff60:
In general for such problems, you can try inspecting the types of your variables.
Typing 0xff01 without quotes, represents a number.
list(range(0xff01, 0xff60)) will give you a list of integers [65281, 65282, .., 65375]. range(0xff01, 0xff60) == range(65281, 65376) evaluates to True.
ord('?') gives you integer 65311.
hex() takes an integer and converts it to '0xff01' (a string).
So, you simply need to use ord(), no need to hex() it.
Just only use ord:
if ord(char) in range(0xff01, 0xff60):
...
hex is not needed.
As mentioned in the docs:
Convert an integer number to a lowercase hexadecimal string prefixed with “0x”.
Obviously that already describes it, it becomes a string instead of what we want, an integer.
Whereas the ord function does what we want, as mentioned in the docs:
Given a string representing one Unicode character, return an integer representing the Unicode code point of that character. For example, ord('a') returns the integer 97 and ord('€') (Euro sign) returns 8364. This is the inverse of chr().

Why does Excel treat the comma as a number?

If I input =numbervalue(4), I get the unsurprising result "4," if I input =numbervalue("m"), I get the unsurprising result "#value!," if I input =numbervalue("."), I get the unsurprising result "#value!," if I input =numbervalue(","), I get the very surprising result "0." Why is this and are there other characters that do this?
From the docs
If the Decimal_separator and Group_separator arguments are not specified, separators from the current locale are used.
You state you are using U.S. settings. Therefore your group separator is ,
If the group separator occurs before the decimal separator in the Text argument, the group separator is ignored.
As the only character in the string, the , is therefore ignored. That leaves you with an empty string
If an empty string ("") is specified as the Text argument, the result is 0.
So, as expected =numbervalue(",") returns 0

How to check if a String contains two same characters?

In my case a string contains for example something like 2500.00. Also you input a string in the same format for example 250.0 which would be converted to 250.00. These strings will be converted to float and they will be added or subtracted.
Now I want to check if the string contains two "." somewhere for example 2.50.00 or 250..00. In that case an errormessage should be displayed.
Therefore my question is how am I able to check if a string contains two "." characters at any position of the string?
You may check if a dot appears more than once in a string with a simple method checking if the first index of the char is not equal to the index of the last char occurrence:
boolean containsTwoDots(String str) {
return str.indexOf('.') != str.lastIndexOf('.');
}

How to check if there's only numbers in string

how to check if there is only numbers in the string?
I want to skip some code with goto if there's only numbers in the string.
Thanks
try
i := StrToInt( str );
except
{ str is NOT an integer }
end;
A simple google: Pascal Help
StrToInt
Convert a string to an integer value.
Declaration
Source position: sysstrh.inc line 113
function StrToInt( const s: string ):Integer; Description
StrToInt will convert the string Sto an integer. If the string
contains invalid characters or has an invalid format, then an
EConvertError is raised.
To be successfully converted, a string can contain a combination of
numerical characters, possibly preceded by a minus sign (-). Spaces
are not allowed.
The string S can contain a number in decimal, hexadecimal, binary or
octal format, as described in the language reference. For enumerated
values, the string must be the name of the enumerated value. The name
is searched case insensitively.
For hexadecimal values, the prefix '0x' or 'x' (case insensitive) may
be used as

How and when are variant type are converted to regular data types

When the actual data type of a variable will be decided?
For ex:
x=10 here x will hold integer
x="Hello" here x will hold string
My basic question is msgbox "2"+"3" is 23 because these are strings and + is for concatenation so the result is 23
Then how the result of msgbox "2"*"3" becomes 6? where the string will be converted to integers and returns 6
If you are talking about using Visual Basic (you have not specified a language) then here is what I believe is happening:
The MsgBox function is expecting a and Object to turn into a String. (or at least it is trying to convert a String before it is displayed). Since "+" is a legit operator for concatenation, the first example can be directly converted to a String and returned.
In the second example, the asterisk is not a legit String operator, so it then has to attempt to convert your String segments into Integers. It does, then multiplies them, then the MsgBox converts the numerical expression back into a String and displays it.

Resources