How to convert double to integer in scriban - scriban

I am trying to use '*' operator in scriban to have a string being concatenated several times like this:
{{
number = 4 / 2
'text' * number
}}
However this gives me an exception: Operator * is not supported for the expression. Only working on string x int or int x string. However variable number obviously contains string. How to fix the template?

In scriban operator '/' always produces double, regardless of the outcome of operation. To have an integer you need to use '//' operator instead:
{{
number = 4 // 2
'text' * number
}}
In fact operator '//' appears to be the only way of converting double to integer in scriban. If you have a ready value that only needs convertion you can always divide it with '//' by 1:
{{
number = number // 1
'text' * number
}}

Related

What does minus and plus notations do on string in c++

I am asking about the job of minus and plus notation on string , in this situation specifically :
Solver(string s) {
for (unsigned int i = 0; i < s.length(); i++) {
grid[i] = (int) (s[i] - '0'); // the minus here will remove 0's of string or not ?
}
}
int main() {
Solver ss(
(string) "850002400" + // the plus here will combine all strings together like Java or not ?
(string) "720000009" +
(string) "004000000" +
(string) "000107002" +
(string) "305000900" +
(string) "040000000" +
(string) "000080070" +
(string) "017000000" +
(string) "000036040"
);
}
operator+ for string concatenates them – as you discovered already. But there's no operator- for strings!
Have a close look, you are not subtracting from the string (s - '0'), but from the character s[i]. This won't remove the character from the string, but instead calculate a new value based on the character's value minus the value of zero character (which has a value of 48, in ASCII and compatible, at least – not the value null!). As digits are guaranteed to be contiguous by C++ standard (just like in C as well), you can reliably calculate decimal digits from characters that way.
This works for bases smaller than 10, too, but not larger ones, as next characters used for representation don't follow the decimal digits directly (and you might have to distinguish upper and lower case letters).
Side note: You don't need the cast to int: as type char is smaller in size than int, both operands will be promoted to int implicitly, so actually the calculation is done in int anyway and the result remains int...
string - C++ Reference
http://www.cplusplus.com/reference/string/string/
As is said in link above, the operator+ means "Concatenate strings".
If you want to CLIP the string, you can use the s.substr() function.
grid[i] = (int) (s[i] - '0')
the the minus in code means transform the 'char' to 'int'. For example,
string s="425";
char c = s[0]; //c='4';
int value = c-'0'; //value=4 it is a number
It is not the function of string, just a utilization of ASCII.
The function stoi(s[i]) can realize the same thing.
Subtracting '0' from any character of digit will return the integer value of that digit.
char seven = '7';
int value = (int)(seven - '0');
cout<<value<<endl;
Output:
7
In your example - was used to convert a character grid(1D) to integer grid(1D).
On the other hand, + sign between two or more string type data represent concatenation of string.
string s = "abc" + "def";
cout<<s<<endl;
Output:
abcdef

I am having problem with this question my output is not correct

Write a program to display the sizes of basic four datatypes i.e integer, double, float, character.
Input:
The first line of input contains integer T denoting the number of test cases. For each test case, the user can input any of the above data types.
Output:
For each test case, there is a single line output displaying the size of that data type.
Constraints:
1<=T<=100
Example:
Input:
4
1
#
7.98
9.985647851
Output:
4
1
4
8
I tried this
int main() {
//code
int x;
cin>>x;
std::string s;
for(int i = 0;i<x;i++){
cin>>s;
cout << sizeof(s) << endl;
}
return 0;
}
Output was
32
32
Wrong Answer
I started to work on this problem to provide the OP with some insight however I have come to conclusion that many others have within the comment section of the OPs original question: and that is that this question or problem has an indeterminate solution!
Reasoning:
If the user enters any of the following digits as a single entity for input { 0, 1, ... 9 } into the console this can be at the least interpreted as an int or a char type and at worst even a possible double, but without the '.' character being present we could eliminate that as a candidate. This problem definitely has ambiguity to it. Checking to see if it is a float or a double is easy; all one has to do is check the string to see if there is at least a '.' then it's either a double or a float then check the last character of the string to see if it is a f and if so then it is a float. Checking to see if it is a character that is a non digit is easy, however distinguishing a single character digit between a char and an int is the troublesome part!
Work around:
You could conclude that if the input string is a single character and is a non digit then it is definitely a char type.
You could conclude that if the input string is a single character and is a digit ASCII[48 - 57] then you could conclude that it is an int type. This would be considered a restraint.
You could conclude that if it isn't the above two it is at least a float or a double and it is a float if and only if the last character of the string is a f, but a . must be present for it to be either of the two. Again these would be restraints that you would put on the accepted data input.

Modulus of same number returns different results

int a=032302;
cout<<a%10<<endl; // output 6
int b=32302;
cout<<b%10<<endl; // output 2
I was trying to get the unit's place of a number but while coding i found a weird thing, the first and the second no are technically same, however they both output different results.
The first one returns 6 while the second one 2 , am i missing something here?
Starting a numeral with 0 (zero) in c/c++ means it is an octal (base 8) number. Thus 032302 is 13506 in decimal notation. Hence, the last digit is 6 and that is what you get from your modulus operation.
Considering the fact that
int a = 032302;
and
int b = 13506;
are holding the same integer value since variable a is init as octal literal
then is correct that
a%10 returns 6 same as b%10 returns 6

How to convert single char to double

I try to create a program that can evaluate simple math expression like "4+4". The expression is given from the user.
The program saves it in a char* and then searches for binary operation (+,-,*,:) and does the operation.
The problem is that I can't figure out how to convert the single char into a double value.
I know there is the atof function but I want to convert single char.
There is a way to do that without creating a char*?
A char usually represents a character. However, a single char is simply an integer in range of at least [-127,+127] (signed version) or at least [0,255] (unsigned version).
If you obtained a character looking as a digit, the value stored in it is an ASCII number representing it. Digits start at code 48 (for zero) and go up incrementally till code 57 (for nine). Thus, if you take the code and subtract 48, you get the integer value. From there, converting it to double is a matter of casting.
Thus:
char digit = ...
double value = double(digit - 48);
or even better, for convenience:
char digit = ...
double value = double(digit - '0'); //'0' has a built-in value 48
There is a way to do that without creating a char* ???
Sure. You can extract the digit number from a single char as follows:
char c = '4';
double d = c - '0';
// ^^^^^^^ this expression results in a numeric value that can be converted
// to double
This uses the circumstance that certain character tables like ASCII or EBCDIC encode the digits in a continuous set of values starting at '0'.

Regular expression with decimal point

I am trying to validate a textbox with a regular expression...
regex expression=(\d{0,4})?([\.]{1})?(\d{0,2})
I am having a problem with the decimal point. the decimal point is optional. the regex should validate for only one decimal point.
example 1.00 ,23.22 , .65 is valid
1.. or 23.. is invalid.
Any suggestions for improving my regex??
Try this one : ^\d{1,4}(\.\d{1,2})?$
It should match :
1
200
9999
12.35
522.4
But not :
1000000
65.
.65
10.326
65..12
Edit :
If you want to match 65. or 9999. use this one instead (see comments) :
^\d{1,4}(\.(\d{1,2})?)?$
Use Application Logic Instead
While you could certainly construct a regular expression for this, it seems simpler to check for a data type or class, or simply scan your input for decimals and then count them. For example, using Ruby:
Check that value is a float or integer.
# Literal value is a float, so it belongs to the Float class.
value = 1.00
value.class == Fixnum or value.class == Float
=> true
# Literal value is an integer, so it belongs to the Fixnum class.
value = 23
value.class == Fixnum or value.class == Float
=> true
Count decimals and make sure there's no more than one.
# Literal value is a float. When cast as a string and scanned,
# only one decimal should be found.
value = 23.22
value.to_s.scan(/\./).count <= 1
=> true
# The only way this could be an invalid integer or float is if it's a string.
# If you're accepting strings in the first place, just cast all input as a
# string and count the decimals it contains.
value = '1.2.3'
value.to_s.scan(/\./).count <= 1
=> false