Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

Amount text box...
the number is positive and this field should be sent as cents ( $22.00 should be sent as 2200 with no decimal).
How can I ?
in my first form page (where there is a textbox/ field )
I use

C#
private void SetPageState()
        {
         int amountInt = int.Parse(mamountTextBox.Text);
         mFormPageState.AmountContribution = amountInt.ToString("0.00");
        SavePageState();
        }

        public struct FormPageState
        {
        public int amountInt;
        public string AmountContribution;
        }


The user enter, for example, 22
and the second screen, I can display as I want:
22.00 with the following code

mAmountLabel.Text = s.AmountContribution;



So I must transform this number (for example 22.00) to 2000 with no decimal for sending on the transaction server

How can I? could you help me please?
Posted
Updated 1-Feb-11 7:26am
v2

I cannot believe the number of answers here which suggest the use of Double variables to capture a financial value, when it is well known that this can seriously affect the final outcome when such a value is used in calculations. Financial values should only ever be captured as either integer or Decimal (if the latter exists).
 
Share this answer
 
Try something like:

C#
string cost = "22.00";
double doubleVal = 0.0;
if (Double.TryParse(cost, out doubleVal))
{
    int newVal = (int)doubleVal * 100;
    // save this value 

}


[note: a bug in the site's pre-tag formatting forces me to enter an extra line before the closing brace]
 
Share this answer
 
v6
Comments
Sergey Alexandrovich Kryukov 1-Feb-11 13:28pm    
OP commented:

Hi Nishant Sivakumar,

I have some error :

Cannot implicitly convert type 'int' to 'string' on ,line 128

Collapse

Line 126: {
Line 127: int newVal = (int)doubleVal * 100;
Line 128: mPriceLabel.Text = newVal;
..............
Sergey Alexandrovich Kryukov 1-Feb-11 13:29pm    
You're trying to assign integer to text property => fail :-)
--SA
Nish Nishant 1-Feb-11 13:35pm    
Hey, why did you change the variable name from cost to const? Confused!
Sergey Alexandrovich Kryukov 1-Feb-11 19:46pm    
Or, because I'm stupid, that's why.
Sorry!
--SA
Nish Nishant 1-Feb-11 13:36pm    
I've changed it back to cost. But what were you trying to do? Perhaps the edit was accidental?
Hi Nishant, Markus and Sakryukov,

It works, many thanks :laugh: :) :thumbsup:

here is a solution

<pre lang="cs">private FormConfirmationPageState mFormConfirmationPageState;

            string cost;
            int newVal;

private void LoadLabels()
        {
            object FormPageState = Session["FormPage"];
            totoForm.FormPageState s = (totoForm.FormPageState)FormPageState;


            mMontantLabel.Text = s.MontantContribution;//  for  10.00
            //changement montant le format DEBUT----------------------------------------
            cost = s.MontantContribution;
            double doubleVal = 0.0;
            if (Double.TryParse(cost, out doubleVal))
            {
            newVal = (int)doubleVal * 100;
            mPriceLabel.Text = newVal.ToString();   //for :  1000

            }
        }

// (....)
        {
        SW.WriteLine(newVal.ToString() + ";" + DateTime.Now.ToString("yyyy'/'MM'/'dd' - 'HH':'mm':'ss") + ";");
                                        SW.Close();
        }

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900