Converting string to integer without losing the decimals number(s) - jinja2

I am trying to convert a string in to an integer in a Jinja template without losing the decimal numbers
I tried the following code which partially works;
{{myfield|int}}
The problem is that I loose any decimal numbers (5.5 becomes 5 for example) is there a way to fix this?

You can use float instead of int:
{{myfield | float}}

Related

Ms Access Database Field losing decimal right side digits

I want to store decimal value into my field when you divide
1120/90 = 12.444444444444444444444444444444
This long but I am losing right side digits, I am only getting 13 right side digits - like that:
12.4444444444444
But when I multiply this back again:
12.4444444444444 x 90 = 1119.999999999996
This is not the correct answer; the correct answer is
12.444444444444444444444444444444 x 90 = 1120
Please help me here.
Thanks
You can use Decimal for this:
? CDec(1120) / CDec(90)
12.444444444444444444444444444
? (CDec(1120) / CDec(90)) * 90
1120
I banged on this for a long time but couldn't find a way to prove the following. assuming any operands can be cast to the integer type without loss of information (1120 & 90 can) then you usually must do the calculation simultaneously: The floating point division operator / handles integer like operands correctly but returns the type double. chained operations are also handled correctly if done all at once. Hence (1120/90) * 90 is calculated correctly as 1120 but also, typename(1120/90*90) returns the double datatype. if you store the intermediate value 1120/90 you get a double of 12.44444 repeating which isn't quite 1120/90.
My suggestion is to store both the operands rather than reducing them to one number. Then do the calculation all at once.

MySQL Column Type for Variable Length Decimal Numbers

I'm trying to find the best column type to use for numeric values of varying length both before and after the decimal place.
The DECIMAL type seems to only allow a fixed-length number with a fixed-length also after the decimal. I need something that preserves to exact precision numbers like:
1.50
222.05
124.2584879775435298
5344.87987797797979077
I can't find anything other than varchar that clearly accommodates this. Am I missing something?
Do you need these numbers for future numeric operations? If not, then you should be able to store the data as a variable length character string so VARCHAR could work.
here this website may answer your questions:
http://dev.mysql.com/doc/refman/5.6/en/numeric-types.html

How to properly define the [float] data type in mysql

I defined (through PHPMYADMIN) a field with the float data type.
When I try to insert a number as 78.556677099932222377 it will translate it to 79.
How do I make it to save the correct number, at least 7 places after the decimal dot?
Open phpMyAdmin with structure option and define the float as:
FLOAT(23,19)
clear in this picture:
How do you define the float in phpMyAdmin
FLOAT(23,19)
when u declare the field you could use that above.
Better to go with Cygnusx1 and change to decimal
see MySQL numeric types
and also Problems with Float
DECIMAL is what you looking for.
ref:
SQL Decimal is used to specify the values in decimal datatype. The Decimal datatype can be of two types : Decimal (p) refers to floating point and Decimal fixed point (p,s). The DECIMAL data type can store decimal floating-point numbers up to a maximum of 32 significant digits and Decimal fixed point can store upto 16 significant digits.

MySql: convert a float to decimal produce more decimal number then the stored in back.sql file

i want to understand this:
i have a dump of a table (a sql script file) from a database that use float 9,2 as default type for numbers.
In the backup file i have a value like '4172.08'.
I restore this file in a new database and i convert the float to decimal 20,5.
Now the value in the field is 4172.08008
...where come from the 008??
tnx at all
where come from the 008??
Short answer:
In order to avoid the float inherent precision error, cast first to decimal(9,2), then to decimal(20,5).
Long answer:
Floating point numbers are prone to rounding errors in digital computers. It is a little hard to explain without throwing up a lot of math, but lets try: the same way 1/3 represented in decimal requires an infinite number of digits (it is 1.3333333...), some numbers that are "round" in decimal notation have infinite number of digits in binary. Because this format is stored in binary and has finite precision, there is an implicit rounding error and you may experience funny things like getting 0.30000000000000004 as the result of 1.1 + 1.2.
This is the difference between float and decimal. Float is a binary type, and can't represent that value exactly. So when you convert to decimal (as expected, a decimal type), its not exactly the original value.
See http://floating-point-gui.de/ for some more information.

Allow number to start with ZERO when stored in mysql integer field

I need to store phone numbers starting with 0 but whenever i try to store this in MySql table the starting ZERO is removed because no number start with Zero actually.
How to solve this issue? Do I need to change the field type from Integer to another type?
change data type to unsigned-zerofill whatever you are using, float, int, decimal(6,2)... only edit the field to unsigned-zerofill
Phone numbers are not really numbers in the sense that they aren't ordinal. They're just characters - they fact that they are numbers is incidental.
Store them in a varchar and move on :D
Phone numbers can contain other symbols for readability too... a regexp for a phone number looks something like [0-9+-()*#]+. So you need to use a text field for phone numbers, plus some validation.
You can use data type as varchar to solve this.
Phone numbers aren't integers and you will only end up with problems trying to store them as integers, store them as strings instead.
Yes - numeric fields only store the numeric values, not the formatting of those (which paddin with leading zeroes is). You should either
change the field type from integer to varchar or char (if # of digits is ALWAYS the same).
Store the number as integer BUT prepend 0 in your presentation layer as needed.
You can also wrap the number you want with a lead zero with a function. I made this function to add lead zero if the "string" is smaller than 2 digits (it was used to add lead zeroes to hours and minutes)
function leadZero($num) {
if (strlen($num) < 2) {
return "0" . $num;
} else {
return $num;
}
}
If you have say, a number 2 that you want to output as 02, you'd do leadZero(2);
This will only add a zero IF the number is less than 2 digits long ! For instance leadZero(14); will return 14