decimal

c# Decimal to string for currency

走远了吗. 提交于 2019-12-19 05:07:06
问题 To display a currency we do: ToString("0.##") For value 5.00 the output is: 5 For value 5.98 the output is: 5.98 For value 5.90 the output is: 5.9 I need the third case to come out with 2 decimal points, eg: 5.90 How can I do this without it affecting the other results? 回答1: I know this doesn't give you a format that fixes the problem, but it's a simple solution to work around it. (5.00).ToString("0.00").Replace(".00",""); // returns 5 (5.90).ToString("0.00").Replace(".00", ""); // returns 5

Get precise decimal string representation of Python Decimal?

ぃ、小莉子 提交于 2019-12-19 02:51:31
问题 If I've got a Python Decimal , how can I reliably get the precise decimal string (ie, not scientific notation) representation of the number without trailing zeros? For example, if I have: >>> d = Decimal('1e-14') I would like: >>> get_decimal_string(d) '0.00000000000001' However: The Decimal class doesn't have any to_decimal_string method, or even any to_radix_string(radix) (cf: https://docs.python.org/3/library/decimal.html#decimal.Context.to_eng_string) The %f formatter either defaults to

Get precise decimal string representation of Python Decimal?

狂风中的少年 提交于 2019-12-19 02:51:26
问题 If I've got a Python Decimal , how can I reliably get the precise decimal string (ie, not scientific notation) representation of the number without trailing zeros? For example, if I have: >>> d = Decimal('1e-14') I would like: >>> get_decimal_string(d) '0.00000000000001' However: The Decimal class doesn't have any to_decimal_string method, or even any to_radix_string(radix) (cf: https://docs.python.org/3/library/decimal.html#decimal.Context.to_eng_string) The %f formatter either defaults to

How can I print a float with thousands separators?

拥有回忆 提交于 2019-12-18 14:24:12
问题 How can I format a decimal number so that 32757121.33 will display as 32.757.121,33 ? 回答1: Use locale.format(): >>> import locale >>> locale.setlocale(locale.LC_ALL, 'German') 'German_Germany.1252' >>> print(locale.format('%.2f', 32757121.33, True)) 32.757.121,33 You can restrict the locale changes to the display of numeric values (when using locale.format() , locale.str() etc.) and leave other locale settings unaffected: >>> locale.setlocale(locale.LC_NUMERIC, 'English') 'English_United

Conversion of byte array containing hex values to decimal values

房东的猫 提交于 2019-12-18 13:37:46
问题 I am making application in c#.Here i want to convert a byte array containing hex values to decimal values.Suppose i have one byte array as array[0]=0X4E; array[1]=0X5E; array[2]=0X75; array[3]=0X49; Here i want to convert that hex array to decimal number like i want to concatenate first all bytes values as 4E5E7549 and after that conversion of that number to decimal.I dont want to convert each separate hex number to decimal.The decimal equivalent of that hex number is 1314813257.So please

Number (integer or decimal) to array, array to number (integer or decimal) without using strings

核能气质少年 提交于 2019-12-18 13:33:50
问题 Requirement: Convert input integer or decimal to an array and convert array of integers which may include a decimal to a number. Restriction: Do not use string methods or convert input or output to a string during the procedure (a self-imposed restriction followed throughout each version of the code composed). Context and use cases BigInt in available in some browsers, though not a BigDecimal . The conversion from integer or decimal to array and array to integer or decimal should be possible

random Decimal in python

不羁的心 提交于 2019-12-18 13:01:41
问题 How do I get a random decimal.Decimal instance? It appears that the random module only returns floats which are a pita to convert to Decimals. 回答1: What's "a random decimal"? Decimals have arbitrary precision, so generating a number with as much randomness as you can hold in a Decimal would take the entire memory of your machine to store. You have to know how many decimal digits of precision you want in your random number, at which point it's easy to just grab an random integer and divide it.

Behind the scenes, what's happening with decimal value type in C#/.NET?

我怕爱的太早我们不能终老 提交于 2019-12-18 12:54:06
问题 How is the decimal type implemented? Update It's a 128-bit value type (16 bytes) 1 sign bit 96 bits (12 bytes) for the mantissa 8 bits for the exponent remaining bits (23 of them!) set to 0 Thanks! I'm gonna stick with using a 64-bit long with my own implied scale. 回答1: Decimal Floating Point article on Wikipedia with specific link to this article about System.Decimal. A decimal is stored in 128 bits, even though only 102 are strictly necessary. It is convenient to consider the decimal as

Best way to get whole number part of a Decimal number

人盡茶涼 提交于 2019-12-18 10:35:13
问题 What is the best way to return the whole number part of a decimal (in c#)? (This has to work for very large numbers that may not fit into an int). GetIntPart(343564564.4342) >> 343564564 GetIntPart(-323489.32) >> -323489 GetIntPart(324) >> 324 The purpose of this is: I am inserting into a decimal (30,4) field in the db, and want to ensure that I do not try to insert a number than is too long for the field. Determining the length of the whole number part of the decimal is part of this

Convert DB2 SQL Decimal to DATE

拟墨画扇 提交于 2019-12-18 09:02:26
问题 I need to convert Decimal to date. I have a decimal date field that contains data like this : 1,132,009.00 --1/13/2009 7,152,004.00 --7/15/2004 11,012,005.00 --11/01/2005 etc I would like it to read as xx/xx/xxxx . Is there anyway to do this with SQL commands or DB2 logic in a select statement? SELECT column1 from table1 ; 回答1: WITH x(decvalue) AS ( VALUES (DECIMAL(1132009.00)),(DECIMAL(7152004.00)),(DECIMAL(11012005.00)) ) SELECT CAST( LPAD( RTRIM( CHAR( INTEGER( decvalue/1000000 ))), 2, '0'