decimal

How to set global rounding for DecimalFields in Django project?

烂漫一生 提交于 2019-12-25 17:46:49
问题 I found similar question with answers, but "setting the option in the settings.py" and "setting getcontext() in apps.py" doesn't work. Standard decimal rounding is ROUND_HALF_EVEN, but I need to set ROUND_HALF_UP. And I repeat my question with a quote - "where in the Django application I have to set rounding option, so that it would work globally in the project?" 回答1: For django project can work setting decimal.DefaultContext (py3, py2). This context is most useful in multi-threaded

Rails: change the value of a an attribute in params

亡梦爱人 提交于 2019-12-25 12:41:11
问题 I have a form with an input( price ) that a user fills with values like: 12,50 or 110,90 (French prices). So in the controller I do : @quote = current_user.company.quotes.build!(params[:quote]) The problem is that rails behaives with the decimals in the US way. So it saves the quote with the price 12.00 or 111.90 So how do I tell rails to actually consider the european version of decimals? Thanks. UPDATE The solution is to add this method to the Quote model. def price=(data) write_attribute(

Weird rounding issue

血红的双手。 提交于 2019-12-25 08:22:02
问题 Something very weird is happening with decimals and floating numbers and I can't understand why or where (ruby/rails/postgresql). Given a purchases table with a decimal column - total : p1 = Purchase.where(total: 5.99).first_or_create p2 = Purchase.where(total: 5.99).first_or_create [p1.id, p2.id] # => [1, 2] p3 = Purchase.where(total: 5.99.to_d).first_or_create p4 = Purchase.where(total: 5.99.to_d).first_or_create [p3.id, p4.id] # => [1, 1] Both Ruby and postgresql have no problem

How to convert decimal literals into hex in C, 500 to 0x0500

十年热恋 提交于 2019-12-25 06:46:08
问题 I have the following: int num=500; char text[8]; how do I make it so that text ends up being the hex 0x500, or 1280? edit: i see that its pretty simple to make it text, like in some of the answers. But I need this text to be interpreted as a hex by C. So in reality it should be an unsigned hex int. 回答1: This should do it. int num = 500; char text[8]; sprintf(text, "0x%d", num); // puts "0x500" in text This is assuming you on purposely didn't convert num to hexadecimal, if this wasn't on

Proper JS currency format with commas and decimals

纵然是瞬间 提交于 2019-12-25 05:02:02
问题 I have looked through countless threads here and elsewhere for over 2 days and I cannot get this to work properly. I have a calculator working exactly the way I need it to, however, I can't seem to get one last thing complete. Comma separator by thousands with decimal. I have the decimal places, but can't add commas. When I do add commas, I can get one or two fields to work before the calculation breaks or the value is displayed as NaN. Here is the working page: http://codepen.io/anon/pen

PHP decimal to binary conversion without decbin()

♀尐吖头ヾ 提交于 2019-12-25 03:55:07
问题 I'm trying to make decimal to binary convertor without using decbin() function. I tried smth like this (which is ok but it needs to be reversed): $dec=101; while ($dec>=1){ $bin = $dec % 2; $dec = round($dec/2, 0, PHP_ROUND_HALF_DOWN); print "$bin"; }// output:1010011 How could I reverse the output to 1100101? Thanks in forward. :) 回答1: To convert a decimal integer to binary, repeatedly divide it by 2 — using integer division — until it becomes 0. The remainders at each step, which are 0s and

Parse a number with dot as thousand separator and comma as decimal separator

孤街醉人 提交于 2019-12-25 03:53:33
问题 I am working with CSV files and Excel often format prices like this: $ 384.642,54 Where the Decimal is 54. I need to round this number to 384.643 (always round up). What I've done is first: Remove spaces and the $ sign that excel puts in the cell. What I have now is this number 384.642,54 . I need PHP to know the decimal is the Coma and the dot is the thousand separator, then round it. I haven't been able to achieve this with number_format. It returns me 384 回答1: There you go: $number = str

Parse a number with dot as thousand separator and comma as decimal separator

不问归期 提交于 2019-12-25 03:53:06
问题 I am working with CSV files and Excel often format prices like this: $ 384.642,54 Where the Decimal is 54. I need to round this number to 384.643 (always round up). What I've done is first: Remove spaces and the $ sign that excel puts in the cell. What I have now is this number 384.642,54 . I need PHP to know the decimal is the Coma and the dot is the thousand separator, then round it. I haven't been able to achieve this with number_format. It returns me 384 回答1: There you go: $number = str

Keep decimal places when open CSV

自闭症网瘾萝莉.ら 提交于 2019-12-25 03:38:30
问题 I have a .csv file looks like this: "13,423.354679", When I open it in Excel it looks like this: 13,423.35 I can let it show correctly by changing the format manually. Is there a way I can keep it shown correctly without formatting in Excel? 回答1: You can show as many as you want, including none. AND use the 1000 separator option to split the numbers like you want = 13,423.35. And yes you can't normally have the actual commas, the formating option just puts them there for visual appearance -

How to remove the .0 when reading CSV with Pandas

走远了吗. 提交于 2019-12-25 01:27:24
问题 I have a CSV file im reading into pandas dataframe. All the numbers do not have any decimal places, but as soon as I read it into the dframe it adds a trailing zero to the number with a decimal. 1205 becomes 1205.0 How do I get rid of the 0 during pd.read_csv? I know i can drop the .0 after it has been read into the dataframe, but i really need it not to happen at all. I have tried float_precision='round_trip' I have tried to force the dtype during read_csv Some of the code i tried: df = pd