decimal

Why does 230/100*100 not return 230? [duplicate]

邮差的信 提交于 2019-12-21 08:17:52
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is JavaScript’s Math broken? In Javascript, I cannot figure out why 230/100*100 returns 229.99999999999997 , while 240/100*100 returns 240. This also applies to 460, 920 and so on... Is there any solution? 回答1: In JavaScript all numeric values are stored as IEEE 754 64-bit floating-point values (also known as double in many languages). This representation has only finite precision (so not all numbers can be

MySQL - How do I update the decimal column to allow more digits?

我是研究僧i 提交于 2019-12-21 07:14:41
问题 I'm a beginner in MySQL, and I accidentally created a table with a column named (price decimal(2,2)); It needs to be decimal(4,2) to allow 4 digits. Since I've already created it, what is the easiest way to update that decimal value to decimal(4,2) ? Or do I need to drop that column completely, and re-create it with the correct numbers? I can't seem to get the syntax right. Thank you very much. 回答1: ALTER TABLE mytable MODIFY COLUMN mycolumn newtype example: ALTER TABLE YourTableNameHere

Python Decimal - engineering notation for mili (10e-3) and micro (10e-6)

橙三吉。 提交于 2019-12-21 03:53:16
问题 Here is the example which is bothering me: >>> x = decimal.Decimal('0.0001') >>> print x.normalize() >>> print x.normalize().to_eng_string() 0.0001 0.0001 Is there a way to have engineering notation for representing mili (10e-3) and micro (10e-6)? 回答1: Here's a function that does things explicitly, and also has support for using SI suffixes for the exponent: def eng_string( x, format='%s', si=False): ''' Returns float/int value <x> formatted in a simplified engineering format - using an

python decimal comparison

痞子三分冷 提交于 2019-12-21 03:36:33
问题 python decimal comparison >>> from decimal import Decimal >>> Decimal('1.0') > 2.0 True I was expecting it to convert 2.0 correctly, but after reading thru PEP 327 I understand there were some reason for not implictly converting float to Decimal, but shouldn't in that case it should raise TypeError as it does in this case >>> Decimal('1.0') + 2.0 Traceback (most recent call last): File "<string>", line 1, in <string> TypeError: unsupported operand type(s) for +: 'Decimal' and 'float' so does

Truncate a floating point number without rounding up

拟墨画扇 提交于 2019-12-21 03:28:21
问题 I have a floating point number that I want to truncate to 3 places but I don't want to round up. For example, convert 1.0155555555555555 to 1.015 (not 1.016 ). How would I go about doing this in Ruby? 回答1: Assuming you have a float , try this: (x * 1000).floor / 1000.0 Result: 1.015 See it working online: ideone 回答2: You can also convert to a BigDecimal, and call truncate on it. 1.237.to_d.truncate(2).to_f # will return 1.23 回答3: Since ruby 2.4 Float#truncate method takes as an optional

can't add two decimal numbers using jQuery

浪尽此生 提交于 2019-12-20 18:44:10
问题 I am trying to add two decimal values but the returned sum is pure integer. What is wrong? I can't find it. any help will be welcome. jQuery(".delivery-method #ship_select").change(function(){ var cost = jQuery(this).val(); jQuery("#delivery_cost").val(cost); //returns 20.00 var tot = parseInt(cost) + parseInt(total); //total returns 71.96 }); With the code i am getting only 91 and not 91.96 回答1: Use parseFloat() instead of parseInt() . jQuery(".delivery-method #ship_select").change(function(

conversion from float to Decimal in python-2.6: how to do it and why they didn't do it

安稳与你 提交于 2019-12-20 18:07:56
问题 Direct conversion from float to Decimal was implemented in python-2.7, both in Decimal's constructor and with the Decimal.from_float() classmethod. Python-2.6 instead throws a TypeError suggesting to convert to string first: TypeError: Cannot convert float to Decimal. First convert the float to a string so my usual workaround is this: if sys.version_info < (2, 7): Decimal.from_float = classmethod(lambda cls, x: cls(str(x))) That's just a literary translation from the error message - and I

Decimal stores precision from parsed string in C#? What are the implications?

心不动则不痛 提交于 2019-12-20 17:42:37
问题 During a conversation on IRC, someone pointed out the following: decimal.Parse("1.0000").ToString() // 1.0000 decimal.Parse("1.00").ToString() // 1.00 How/why does the decimal type retain precision (or, rather, significant figures) like this? I was under the impression that the two values are equal, not distinct. This also raises further questions: How is the number of significant figures decided during mathematical operations? Does the number of significant figures get retained during

What is the purpose of Decimal.One, Decimal.Zero, Decimal.MinusOne in .Net

浪子不回头ぞ 提交于 2019-12-20 10:28:39
问题 Simple question - why does the Decimal type define these constants? Why bother? I'm looking for a reason why this is defined by the language, not possible uses or effects on the compiler. Why put this in there in the first place? The compiler can just as easily in-line 0m as it could Decimal.Zero, so I'm not buying it as a compiler shortcut. 回答1: Small clarification. They are actually static readonly values and not constants. That has a distinct difference in .Net because constant values are

Python Decimal to String

会有一股神秘感。 提交于 2019-12-20 10:17:56
问题 There are tons of topics on here that explain how to convert a string to a decimal, but how do I convert a decimal back to a string? Like if I did this: import decimal dec = decimal.Decimal('10.0') How would I take dec and get '10.0' (a string) out? 回答1: Use the str() builtin, which: Returns a string containing a nicely printable representation of an object. E.g: >>> import decimal >>> dec = decimal.Decimal('10.0') >>> str(dec) '10.0' 回答2: Use the string format function: >>> from decimal