Python Decimals format

If you have Python 2.6 or newer, use format: ‘{0:.3g}’.format(num) For Python 2.5 or older: ‘%.3g’%(num) Explanation: {0}tells format to print the first argument — in this case, num. Everything after the colon (:) specifies the format_spec. .3 sets the precision to 3. g removes insignificant zeros. See http://en.wikipedia.org/wiki/Printf#fprintf For example: tests=[(1.00, ‘1’), (1.2, ‘1.2’), … Read more

Two decimal places using printf( )

What you want is %.2f, not 2%f. Also, you might want to replace your %d with a %f 😉 #include <cstdio> int main() { printf(“When this number: %f is assigned to 2 dp, it will be: %.2f “, 94.9456, 94.9456); return 0; } This will output: When this number: 94.945600 is assigned to 2 dp, … Read more

How to calculate difference in hours (decimal) between two dates in SQL Server?

DATEDIFF(hour, start_date, end_date) will give you the number of hour boundaries crossed between start_date and end_date. If you need the number of fractional hours, you can use DATEDIFF at a higher resolution and divide the result: DATEDIFF(second, start_date, end_date) / 3600.0 The documentation for DATEDIFF is available on MSDN: http://msdn.microsoft.com/en-us/library/ms189794%28SQL.105%29.aspx

Moving decimal places over in a double

If you use double or float, you should use rounding or expect to see some rounding errors. If you can’t do this, use BigDecimal. The problem you have is that 0.1 is not an exact representation, and by performing the calculation twice, you are compounding that error. However, 100 can be represented accurately, so try: … Read more

C++ – Decimal to binary converting

std::bitset has a .to_string() method that returns a std::string holding a text representation in binary, with leading-zero padding. Choose the width of the bitset as needed for your data, e.g. std::bitset<32> to get 32-character strings from 32-bit integers. #include <iostream> #include <bitset> int main() { std::string binary = std::bitset<8>(128).to_string(); //to binary std::cout<<binary<<“\n”; unsigned long decimal … Read more

Remove trailing zeros from decimal in SQL Server

A decimal(9,6) stores 6 digits on the right side of the comma. Whether to display trailing zeroes or not is a formatting decision, usually implemented on the client side. But since SSMS formats float without trailing zeros, you can remove trailing zeroes by casting the decimal to a float: select cast(123.4567 as DECIMAL(9,6)) , cast(cast(123.4567 … Read more

How to convert a decimal number into fraction?

You have two options: Use float.as_integer_ratio(): >>> (0.25).as_integer_ratio() (1, 4) (as of Python 3.6, you can do the same with a decimal.Decimal() object.) Use the fractions.Fraction() type: >>> from fractions import Fraction >>> Fraction(0.25) Fraction(1, 4) The latter has a very helpful str() conversion: >>> str(Fraction(0.25)) ‘1/4’ >>> print Fraction(0.25) 1/4 Because floating point values … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)