decimal

decimals, ints, casting... oh my!

I'm going through the "Head First C#" book and in one of the chapters I created a program and uses variables declared as ints and decimals. Visual Studio got cranky with me a couple of times about mixing and matching the two. For example: dinnerParty.NumberOfPeople = (int) numericUpDown1.Value; NumberOfPeople is declared as an int an...

How do I convert a decimal to string value for dollars and cents in ruby?

I am storing a cost in my application. The cost is not formatted in the database. For example: 00.00 saves as 0, 1.00 saves as 1, and 40.50 saves as 40.5 I need to read these values from the database and convert them to strings for dollars and cents. For example: 0 --> cost_dollars = "00" & cost_cents = "00", 1 --> cost_dollars = "01...

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

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 shor...

How to read and process binary (base-2) logical representations from file

I have a file containing 800 lines like: id binary-coded-info --------------------------- 4657 001001101 4789 110111111 etc. where each 0 or 1 stands for the presence of some feature. I want to read this file and do several bitwise logical operations on the binary-coded-info (the operations depend on user input and on in...

How do I convert a decimal fraction to binary in Java?

I need to convert 0.5 in base 10 to base 2 (0.1). I have tried using Double.doubleToRawLongBits(0.5) and it returns 4602678819172646912 which I guess is in hex, but it does not make sense to me. ...

Calculate System.Decimal Precision and Scale

Suppose that we have a System.Decimal number. For illustration, let's take one whose ToString() representation is as follows: d.ToString() = "123.4500" The following can be said about this Decimal. For our purposes here, scale is defined as the number of digits to the right of the decimal point. Effective scale is similar but ignores...

Round Off decimal values in C#

how do i round off decimal values ? Example : decimal Value = " 19500.98" i need to display this value to textbox with rounded off like " 19501 " if decimal value = " 19500.43" then value = " 19500 " ...

decimal places and Pow function in c++

Most likely a really simple question, please keep any answers easy to understand I'm still quite new at this: I'm making a little app, and I need to use powers for a couple of calculations. After a little research I found the pow function in cmath, and have had a play. In the end i came up with this snipped, which works: #include <iost...

XSD - xs:nonNegativeInteger and values ending with .00

I realize that the value 0.00 is not a valid nonNegativeInteger, nor is it even an Integer. Unfortunately, the data is coming in that format. I don't want to throw it away if it ends with .0 but, I also don't want to change the type to Decimal and possibly have values ending in .1 coming in as valid. Is there a way my XSD can validate ...

Storing decimal values in SQL Server

I'm trying to figure out decimal data type of a column in the SQL server. I need to be able to store values like 15.5, 26.9, 24.7, 9.8, etc I assigned decimal(18, 0) to the column data type but this not allowing me to store these values. What is the right way to do this? Thank you ...

Ruby Iteration Question : Binary->Decimal Program

I am learning Ruby and thought of making a Binary->Decimal converter. It gets a binary string and converts to decimal equivalent. Is there a way to keep track of the current iteration step in ruby so that the variable 'x' can be removed? def convert(binary_string) decimal_equivalent = 0 x=0 binary_string.reverse.each_char...

Why does Spreadsheet::XLSX::Utility2007's xls2csv round off to two decimal places?

I am writing a Perl/Tk script which displays Excel worksheets using the ss2tk example script from the Spreadsheet::Read module. It does not round off two decimal places but the function from Spreadsheet::XLSX::Utility2007 does round off to two decimal places. Why? I'm trying to use that second function as a feature of my program to offer...

Convert hex character to decimal equivelant in MIPs?

How do I take a single ASCII character and convert it to its decimal equivelant in MIPs? Do I simply have to have some conditions to subtract a certain amount from the ascii code to make it its decimal representation? ...

Efficiently convert between Hex, Binary, and Decimal in C/C++

I have 3 base representations for positive integer numbers: Decimal, in unsigned long variable (e.g. unsigned long int NumDec = 200). Hex, in string variable (e.g. string NumHex = "C8") Binary, in string variable (e.g. string NumBin = "11001000") I want to be able to convert between numbers in all 3 representations in the most effici...

C# Decimal.Parse issue with commas

Here's my problem (for en-US): Decimal.Parse("1,2,3,4") returns 1234, instead of throwing an InvalidFormatException. Most Windows applications (Excel en-US) do not drop the thousand separators and do not consider that value a decimal number. The same issue happens for other languages (although with different characters). Are there any...

Java negative int to hex and back fails

Hello, public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE; String minHex = Integer.toHexString(Integer.MIN_VALUE); System.out.println(min + " " + minHex); System.out.println(Integer.parseInt(minHex, 16)); } } Gives -2147483648 80000000 Exception in thread "main"...

Why do double and float exist?

Duplicate http://stackoverflow.com/questions/803225/when-should-i-use-double-instead-of-decimal .. and many more... We use the C# and SQL Server decimal datatypes throughout our apps because of their accuracy. Never had any of those irritating problems where the total doesn't add up to the detail, etc. I was wonderi...

Is casting narrow types to wider types to save memory and keep high-precision calculations a terrible idea?

I'm dealing with financial data, so there's a lot of it and it needs to be relatively high-precision (64bit floating point or wider). The standard practice around my workplace seems to be to represent all of it as the c# decimal type which is a 128bit wide floating point specifically created to support round-off free base10 operations. ...

Converting very large decimal numbers to hexadecimal (1.67119535743*10^33)

I'm curious as to if it'd be possible to convert a very, very large decimal number such as 1.67119535743*10^33/1.67119535743E+33 to hexadecimal via PHP or C#. All my previous attempts have failed, unfortunately. Thanks to all in advance! ...

Convert really big number from binary to decimal and print it

I know how to convert binary to decimal. I know at least 2 methods: table and power ;-) I want to convert binary to decimal and print this decimal. Moreover, I'm not interested in this `decimal'; I want just to print it. But, as I wrote above, I know only 2 methods to convert binary to decimal and both of them required addition. So, I'...