decimal

Parsing a decimal number

Guys, I have a string that contains a decimal number. The problem is, sometimes it is negative and it is stored in accounting format (positive number surrounded by parenthesis). In other words, I got a string like this: string s = "(35.00)"; What I'm doing currently is: decimal TheValue = decimal.Parse(s); This value of TheValue sh...

Number Format / DecimalDigits

I have code like: lblFranshizShowInvwNoskhehEdit.Text = string.Format("{0:n}", (double)(int.Parse(drDarman["FranshizDarsad"].ToString()) * Convert.ToInt64(RadNumerictxtPayInvwNoskhehEdit.Text)) / 100); But {0:n0} string format forces the label's text to not have decimal digits and {0:n} string format forces the label's te...

Converting double to decimal

I have a problem with conversion from double to decimal: public class CartesianCoordinates { public int LatitudeHours { get; set;} public int LatitudeMinutes { get; set; } public int LatitudeSeconds { get; set; } public GeoDirectionLongtitude LongitudeDirection { get; set; } public int Longi...

PHP Decimal calculations

I was reading that you have to be careful with floating point comparisons and calculations in PHP. I'm not an expert at PHP so I thought I would ask the community what is the best way to do accurate decimal calculations in PHP. I'm developing a timecard application where it calculates all the time entered for the week. Anything over 40 ...

Java DecimalFormat problem

I would like to format numbers of type double with a German locale in Java. However something goes wrong since the output of the following code is : 0.0 package main; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.ParseException; import java.util.Locale; public class Test { private static String d...

Bash Multiplying Decimal to int

I read price from user input. When i multiply the input with int like this T="$((PRICE*QTY))"|bc; gives line 272: 12.00: syntax error: invalid arithmetic operator (error token is ".00") or .50 depending on user input. How do i multiply these two variables and get a total with 2 decimal points? ...

Determine the decimal precision of an input number

We have an interesting problem were we need to determine the decimal precision of a users input (textbox). Essentially we need to know the number of decimal places entered and then return a precision number, this is best illustrated with examples: 4500 entered will yield a result 1 4500.1 entered will yield a result 0.1 4500.00 entered ...

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

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

C#: Storing percentages, 50 or 0.50?

When holding percentage values in variables is there a preference between holding them as whole numbers vs fractions. That is should the variable hold numbers between 0 and 100 or between 0.00 and 1.00? In either case, the variable holding the values is of decimal type. The database I'm interacting with happens to store them as whole n...

How can I allow period and comma as valid decimal point separators for floats?

I would like to allow both "123.45" and "123,45" as valid decimal inputs but currently "123.45" results in "The value '123.45' is not valid for Foo". ...

Using a dynamic precision value in number_to_currency based on the decimal value

Thoughout our app we use number_to_currency(value, :precision => 2). However, we now have a requirement whereby the value may need displaying to three or more decimal places, e.g. 0.01 => "0.01" 10 => "10.00" 0.005 => "0.005" In our current implementation, the third example renders as: 0.005 => "0.01" What's the best approach ...

C# - Greater decimal accuracy than Decimal type

Is it possible to get more decimal precision than the decimal type in C#? According to MSDN, the decimal type can store up to 29 significant digits, however I am wondering if it possible to store more than that and how to go about doing so. Thanks in advance. ...

C# 2.0 double handling - Bizarre behaviour

Double dblValue = 0.0001; Boolean a = (dblValue >= (1 / 1000)); Boolean b = (dblValue >= 0.001); Console.WriteLine("dblValue >= (1 / 1000) is " + a); Console.WriteLine("dblValue >= 0.001 is " + b); Console.ReadLine(); The above C# code evaluates 'a' to true and 'b' to false. In VB.NET, the equivalent code evaluates 'a' to false and 'b'...

Python MySQLdb cursor truncating values

I ran into an interesting problem. I have a MySQL database that contains some doubles with very precise decimal values (for example, 0.00895406607247756, 17 decimal places). This is scientific data so this high level of precision is very important. I'm using MySQLdb in Python to select data from the database: cursor.execute("SELECT * F...

What does the first argument to MySQL's Decimal function do?

Using MySQL 5.0.27 This query: SELECT CAST('543.21' AS DECIMAL(100,2)) returns 543.21 So does this one: SELECT CAST('543.21' AS DECIMAL(2,2)) In fact, I am having trouble figuring out what effect the parameter has. I am using it to aggregate numeric values in a varchar column (for legacy reasons!!) and round off to 2 decimal places. ...

Best practices for doing accounting in Python

I am writing a web2py application that requires summing dollar amounts without losing precision. I realize I need to use Decimals for this, but I've found myself having to wrap every single number I get from the database with: Decimal(str(myval)) Before I go crazy adding that to all of my code, is there a better way? I'm new to Pytho...

Convert ASCII string into Decimal and Hexadecimal Representations

I need to convert an ASCII string like... "hello2" into it's decimal and or hexadecimal representation (a numeric form, the specific kind is irrelevant). So, "hello" would be : 68 65 6c 6c 6f 32 in HEX. How do I do this in C++ without just using a giant if statement? EDIT: Okay so this is the solution I went with: int main() { st...

When I insert 78.9 into Mysql (using JDBC) it gets rounded up to 79? Is this normal

When I insert 78.9 into Mysql (using JDBC) it gets rounded up to 79? Is this normal ... if so how can I stop this from happening. More details: Column name: num Data type: decimal(12,0) * The above item was copied from phpMyAdmin Query is stmt.executeUpdate("INSERT INTO triples(sub_id, pro_id, num) VALUES("+subId+","+proId+",78.9)");...

.NET decimal.Negate vs multiplying by -1

Are there any differences between decimal.Negate(myDecimal) and myDecimal * -1 (except maybe readability)? ...

Issues with checking the equality of two doubles in .NET -- what's wrong with this method?

Hi, So I'm just going to dive into this issue... I've got a heavily used web application that, for the first time in 2 years, failed doing an equality check on two doubles using the equality function a colleague said he'd also been using for years. The goal of the function I'm about to paste in here is to compare two double values to...