goalie35
goalie35

Reputation: 786

Convert decimal to 10-digit number

In my c# application, I have a decimal value (10.0) that I need to insert into a database field (field type BIGINT). The values currently in the db are all 10-digits long.

How can I convert my 10.0 decimal value to 1000000000?

Thanks

Edit: This field is for the order amount being inserted into an order transactions table. Not sure why they have 10-digit values in this field, but there's already millions of records in this table, all containing 10-digits. So I'm just trying to keep my values the same.

Here's some basic add'l details. In the below example, lets say I enter "10.00" into the txtOrderAmount textbox, I will need to convert this into "1000000000" to insert into the database:

My c# application:

decimal orderAmount = 0;
orderAmount = Convert.ToDecimal(txtOrderAmount.Text);
if(orderAmount > 0)
{
     ledger.InsertOrderTransaction(amount);
}

This calls InsertOrderTransaction, which then inserts into the database.

Some sample records from my database table, called "Ledger". The sample values are for $50.00, $10.00, & $13.50 respectively:

LedgerID | EntryDate | OrderID | Amount

2086 | 2-14-2014 | 1123 | 5000000000

2087 | 2-18-2014 | 1197 | 1000000000

2088 | 3-9-2014 | 8741 | 1350000000

Hope that helps. Again, why the original developer did a 10-digit BIGINT here, I have no clue but I just want to keep my values the same as his.

Upvotes: 0

Views: 1831

Answers (3)

BradleyDotNET
BradleyDotNET

Reputation: 61339

The easiest way to accomplish this is to just multiply:

Decimal myBigNumber = smallNumber * 100000000;

This comes with a major caveat: Any numbers >= 100 will break That is, they will be 11 digits long. Also, numbers < 10 will break because they will be 9 digits long.

Because integers store no decimal-place information, there isn't an easy way to fix this without adding a field or converting to a floating-point type. Removing the the 10-99 requirement will make the conversion back to 10.0 impossible.

Upvotes: 0

goalie35
goalie35

Reputation: 786

Thanks for the help. The problem with the answers was the calculation assumes the number will be between 10-99.

I tweaked it a little to be able to calculate any value:

int wholeVal = (int)amount;  //"amount" is a double & the original value from the textbox.  Counvert it to an int
int wholeValLength = wholeVal.ToString().Replace(",", "").Length;  //get the length of int
string calculation = "1";  //number to calculate begins with 1
for (int i = wholeValLength; i < 10; ++i)
{
     //add zeros to the calculation value
     calculation += "0";  
}
double dblCalculation = Convert.ToDouble(calculation);  
ledgerAmount = amount * dblCalculation;  //perform calculation

Upvotes: -1

Rune FS
Rune FS

Reputation: 21742

try

decimal orderAmount = 0;
orderAmount = Convert.ToDecimal(txtOrderAmount.Text);
if(orderAmount > 0)
{
     ledger.InsertOrderTransaction(amount * 100000000);
}

that's a simple multiplication an will treat the decimal as an integer if the InsertOrderTransaction method accepts an integer

Upvotes: 2

Related Questions