decimal

Decimal rounding errors upon division (C#)

∥☆過路亽.° 提交于 2019-12-10 14:04:34
问题 I have basically four numbers (say 100, 200, 300, 400), and I need to calculate the probability as 100/(100+200+300+400), 200/(100+200+300+400), and so on. When I use the decimal data type to store these probabilities, they don't up to one due to round issues. What's the best way to past this without making the probabilities too inaccurate? Basically I do this calculation many many times, so I don't want to have to change all the divisions into Math.Round stuff. :| 回答1: The solution is

Hot to get user’s regional settings for currency in JavaScript or jQuery?

萝らか妹 提交于 2019-12-10 13:54:40
问题 I’m trying to format some numbers with jQuery. I would like to get the user’s regional settings for currency and number, in order to implement the correct format (obtain the decimal separator). Is it possible to retrieve these parameters with jQuery or JavaScript? 回答1: Use toLocaleString() with style:'currency' : var amount = 123.56; alert( 'German: ' + amount.toLocaleString('de-DE',{style:'currency',currency:'EUR'}) + ', ' + 'American: ' + amount.toLocaleString('en-US',{style:'currency'

C# decimal.Parse behaviour

喜欢而已 提交于 2019-12-10 12:59:09
问题 Short question: Why are these ' ..... ' valid for parsing a decimal in .NET (C#): decimal res = decimal.Parse("8......15"); // returns 815 decimal res = decimal.Parse("8...15"); // returns 815 decimal res = decimal.Parse("8..15"); // returns 815 What's the reason for this? 回答1: It fails for me. Are you by any chance in a culture where "." is the thousands separator and "," is the decimal point? Decimal.Parse (and similar calls) use the thread's current culture by default. Whether that is a

typeof() to check for Numeric values

限于喜欢 提交于 2019-12-10 12:54:01
问题 what is the easiest way to check if a typeof() is mathematically usable(numeric). do i need to use the TryParse method or check it by this: if (!(DC.DataType == typeof(int) || DC.DataType == typeof(double) || DC.DataType == typeof(long) || DC.DataType == typeof(short) || DC.DataType == typeof(float))) { MessageBox.Show("Non decimal data cant be calculated"); return; } if there is a more easy way to achieve this, your free to suggest 回答1: There's nothing much to do, unfortunately. But from C#

Passing a Decimal(str(value)) to a dictionary for raw value

寵の児 提交于 2019-12-10 12:27:11
问题 I'm needing to pass values to a dictionary as class 'decimal.Decimal', and the following keeps happening: from decimal import * transaction_amount = 100.03 transaction_amount = Decimal(str(transaction_amount)) item = { 'transaction_amount': transaction_amount } print(item) Results: {'transaction_amount': Decimal('100.03')} How do I attain the raw 100.03 result, rather than Decimal('100.03')? This is what I want the dictionary to have saved: {'transaction_amount': 100.03)} When I do: print

Ruby on Rails Models. Why does a decimal{2,1} with scope 1 allow more than digit after the decimal point?

情到浓时终转凉″ 提交于 2019-12-10 12:22:47
问题 I'm having an issue with a table accepting too many digits after the decimal, despite defining it's precision and scope. rails generate model Hotel name:string 'rating:decimal{2,1}' class CreateHotels < ActiveRecord::Migration def change create_table :hotels do |t| t.string :name t.decimal :rating, precision: 2, scale: 1 t.timestamps end end end However, I am able to do the following. Hotel.create!(name: “The Holiday Inn”, rating: 3.75) Additionally, I have a rooms table (Room model), with t

C: 0x11 in Decimal

蓝咒 提交于 2019-12-10 12:14:19
问题 Sorry about this basic question, but why 0x11 is 17 in decimal (print(%d, 0x11)=17 ? I search information about the way to convert from hex to dec, but it doesn't talk about this sort of numbers. 回答1: Just like "11" in base ten means "1 ten" and "1 one", "11" in base 16 (i.e. hex) means "1 sixteen" and "1 one" - or 17 in base 10. 回答2: 0x at the start of a number means that the compiler will read it as hexadecimal. 0x11 = 1 * 16 + 1 = 17 回答3: 0x11 = 1*16^1 + 1*16^0 = 17. (Like 17 = 1 * 10^1 +

C++ cout list with decimals aligned using setw(x) not put_money

自闭症网瘾萝莉.ら 提交于 2019-12-10 11:37:37
问题 C++ Code runs well, but currently outputting values to right but justified left and not lining up on the decimal. Can't use put_money, what am I missing? Attempted using fprint and put_money, confirmed with classmate we're supposed to use setw(x). #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { const double taxRate = 0.09; const double laborCPH = 35.0; //where CPH is Cost Per Hour double costParts; double costLabor; double totalTax; double totalDue;

Python how to remove decimal?

假装没事ソ 提交于 2019-12-10 11:26:28
问题 I've found some other topics on this but I couldn't get it to work. Please pardon my naivety with Python. Berekening1 = 8.5 Berekening2 = 8.1+4.8 Berekening3 = 8*10 Berekening4 = 3 x = Berekening1 * Berekening2 * Berekening3 + Berekening4 print "Het antwoord van de berekening is:", round(x); print x, print "." I want x to be an integer. How do I do that? I tried both int and round . Anyone also have an idea on how to remove the "space" between x and "." at the end when code is executed? 回答1:

Problems with decimals and scientific notation in Python 2.6.6

你说的曾经没有我的故事 提交于 2019-12-10 11:24:30
问题 I'm having difficulty with decimal values that I need to use for arithmetic in some cases and as strings in others. Specifically I have a list of rates, ex: rates=[0.1,0.000001,0.0000001] And I am using these to specify the compression rates for images. I need to initially have these values as numbers because I need to be able to sort them to make sure they are in a specific order. I also want to be able to convert each of these values to strings so I can 1) embed the rate into the filename