This is something you can try in the interpreter, can anyone explain me why? Note: This problem is different from  because my answers are not a little off, but way off and my question is about decimal, not about the built in float.  from decimal import getcontext, Decimal a = Decimal(".110001"+"0"*17+"1"+"0"*95+"1"+"0"*599+"1"+"0"*4319+"1") b = Decimal(".220002"+"0"*17+"2"+"0"*95+"2"+"0"*599+"2"+"0"*4319+"2") b-a == a # Returns False while it should be True b-a-a # Returns Decimal('-1.000000000000000000000000000E-120')
27
Consider the following code:   0.1 + 0.2 == 0.3  -&gt;  false   0.1 + 0.2         -&gt;  0.30000000000000004   Why do these inaccuracies happen?
99240
I write financial applications where I constantly battle the decision to use a double vs using a decimal.  All of my math works on numbers with no more than 5 decimal places and are not larger than ~100,000. I have a feeling that all of these can be represented as doubles anyways without rounding error, but have never been sure.  I would go ahead and make the switch from decimals to doubles for the obvious speed advantage, except that at the end of the day, I still use the ToString method to transmit prices to exchanges, and need to make sure it always outputs the number I expect. (89.99 instead of 89.99000000001)  Questions:   Is the speed advantage really as large as naive tests suggest? (~100 times) Is there a way to guarantee the output from ToString to be what I want? Is this assured by the fact that my number is always representable?   UPDATE: I have to process ~ 10 billion price updates before my app can run, and I have implemented with decimal right now for the obvious protective reasons, but it takes ~3 hours just to turn on, doubles would dramatically reduce my turn on time. Is there a safe way to do it with doubles?
10027
What would be the most efficient way to compare two double or two float values?  Simply doing this is not correct:  bool CompareDoubles1 (double A, double B) {    return A == B; }   But something like:  bool CompareDoubles2 (double A, double B)  {    diff = A - B;    return (diff &lt; EPSILON) &amp;&amp; (-diff &lt; EPSILON); }   Seems to waste processing.  Does anyone know a smarter float comparer?
78703
If have this in the setter of a property:  decimal? temp = value as decimal?;   value = "90"  But after the cast, temp is null...  What is the proper way to do this cast?
9718
How do you convert decimal values to their hexadecimal equivalent in JavaScript?
7653
public class doublePrecision {     public static void main(String[] args) {          double total = 0;         total += 5.6;         total += 5.8;         System.out.println(total);     } }   The above code prints:  11.399999999999   How would I get this to just print (or be able to use it as) 11.4?
55076
How do you convert a number to a string showing dollars and cents?  eg: 123.45    =&gt; '$123.45' 123.456   =&gt; '$123.46' 123       =&gt; '$123.00' .13       =&gt; '$0.13' .1        =&gt; '$0.10' 0         =&gt; '$0.00'
38011
I am developing a PHP application in which I am in need to store values bigger than 1 million, but with the flexibility of having floating points. I know the traditional float and double. I am looking to store only up to two decimal points, and will only handle operations with numbers that have 2 decimals exclusively.  I have chosen my column type of double and unsigned, but whenever I store a row with a value bigger than one million, it gets truncated to 999999.99  Why is this? I am looking for the right datatype/solution. If you could point me in the right direction, I will appreciate it a lot!  Thank you for your time in advance!  Cheers.
29737
I'm writing an application which reads large arrays of floats and performs some simple operations with them. I'm using floats, because I thought it'd be faster than doubles, but after doing some research I've found that there's some confusion about this topic. Can anyone elaborate on this?
73750
I need to find out how to format numbers as strings. My code is here:  return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm   Hours and minutes are integers, and seconds is a float.  the str() function will convert all of these numbers to the tenths (0.1) place.  So instead of my string outputting "5:30:59.07 pm", it would display something like "5.0:30.0:59.1 pm".  Bottom line, what library / function do I need to do this for me?
82626
Suppose,I have a number 1256. I want (1256 % 11)! That is 2 . But, here, if i try in this way,  ( (125 % 11 ) * 10 + 6 ) % 11 = 2. That is exactly the same answer 2 as above.  I am confused , how This two process gives the similar answer ??? Even if , I try this method for any number n,m  to find (n mod m) . How this works ?? Can anybody explain ?
102229
A quick example of what I want using bash scripting:  #!/bin/bash echo "Insert the price you want to calculate:" read float echo "This is the price without taxes:" echo "scale=2; $float/1.18" |bc -l read -p "Press any key to continue..." bash scriptname.sh   Assuming that the price is: 48.86 The answer will be:41.406779661 (41.40 actually because I'm using scale=2;)  My Question is: How I round the second decimal to show the answer in this way?: 41.41
3860
I have a text box that will have a currency string in it that I then need to convert that string to a double to perform some operations on it.   "$1,100.00" → 1100.00  This needs to occur all client side. I have no choice but to leave the currency string as a currency string as input but need to cast/convert it to a double to allow some mathematical operations.
8128
What is the best possible way to check if a string can be represented as a number in Python?   The function I currently have right now is:  def is_number(s):     try:         float(s)         return True     except ValueError:         return False   Which, not only is ugly and slow, seems clunky. However I haven't found a better method because calling float in the main function is even worse.
61612
I'm looking for a  way to convert numbers to string format, dropping any redundant '.0'   The input data is a mix of floats and strings. Desired output:  0   --> '0'  0.0 --> '0'  0.1 --> '0.1'  1.0 --> '1'  I've come up with the following generator expression, but I wonder if there's a faster way:  (str(i).rstrip('.0') if i else '0' for i in lst)   The truth check is there to prevent 0 from becoming an empty string.  EDIT: The more or less acceptable solution I have for now is this:  ('%d'%i if i == int(i) else '%s'%i for i in lst)   It just seems strange that there is no elegant way to handle this (fairly straightforward) case in python.
7604
I keep stumbling on the format specifiers for the printf() family of functions.  What I want is to be able to print a double (or float) with a maximum given number of digits after the decimal point.  If I use:  printf("%1.3f", 359.01335); printf("%1.3f", 359.00999);   I get  359.013 359.010   Instead of the desired  359.013 359.01   Can anybody help me?
63306
How can we extract the decimal part of a floating point number and store the decimal part and the integer part into two separate integer variables?