locked
Convert.ToDecimal function RRS feed

  • Question

  • User-1746179790 posted

     Hi all guys

    My problem is that i have string "28538.38", when i convert.todecimal("28538.38") and insert into table, the value in the table is 2853838, i want it is the same value 28538.38 (my field is decimal type).

    How can i fix it?

    thank in advanced.

    Friday, November 19, 2010 9:58 PM

Answers

  • User759660354 posted

    ok try to change the string  "28538.38" to "28538,38" (Comma instead of dot);
    run your program again and see if the value gets correct.

    Check if the decimal value is correct.

    And if your store it in a database check if the database value gets correct.

    If yes your problem is definitly a localization problem (Either client webserver or db-server related).


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 21, 2010 2:48 PM

All replies

  • User1949565384 posted

    You might have defined SQL datatype with Decimal(18,0).

    You should change it to somet thing like Decimal(10,2) which will allow total of 10 character and to digit after decimal point.


    Let me know if you have any queries.


    Thanks,

    Ashish


    Friday, November 19, 2010 10:49 PM
  • User-697637982 posted

    i want it is the same value 28538.38 (my field is decimal type).

    Hi,


    In case you still need to change the table's column type you can use   T-SQL , the sintax for MS SQL is:


    ALTER TABLE table ALTER COLUMN column_name new_data_type 
    Ex.
    ALTER TABLE MyTable ALTER COLUMN MyColumn decimal(16, 2)

    try this or u can go to enterprise Manager and went to design view of the table and changed the data type of the field.

    Friday, November 19, 2010 11:59 PM
  • User-1746179790 posted

    my string is Salerate="28538.38"

    i have changed my field to decimal(10,2)  the value in table is 28538.00,  if i changed my field to money type the value in the table is 2853838.00 

    How can i fix it?

     

    Saturday, November 20, 2010 12:49 AM
  • User1949565384 posted

    Try with Numeric(10,2) datatype. That should resolve your issue.


    Thanks,

    Ashish

    Saturday, November 20, 2010 1:23 AM
  • User-1746179790 posted

     if i changed to Numeric(10,2), the same problem with money type, the value in the table is 2853838. (i hope it is 28538.38, the same with my string input).

    Other solution?

    Saturday, November 20, 2010 1:38 AM
  • User1949565384 posted

    Can you please post you code?


    Saturday, November 20, 2010 2:23 AM
  • User759660354 posted

    please check your localization settings in your aspx page or in the web.config. 

    <globalization uiCulture="auto" culture="en-US" />
    You can set them for the client and/or for the server.
    I believe this is the issue. 
    If not you need to use localization in the convert.todecimal statement
     


     

    Saturday, November 20, 2010 2:34 AM
  • User-1746179790 posted

    I defind function is called InsertExchangeRate in BLL to run sql store pro in database like this

     

    public

    {

     

     

     

     

     

    }

    static int InsertExchangeRate(string title, decimal buyrate, decimal salerate)

     then in my app page i call this function

    String SaleRate = "28538.38";
    ExchangeRate.InsertExchangeRate(Title, Convert.ToDecimal(BuyRate),Convert.ToDecimal(SaleRate));

    now i try to change my field to float  and my variable to Double in order check it. but we still find solution for that problem.

    pls help

     

    Saturday, November 20, 2010 2:57 AM
  • User-1746179790 posted

    if i change culture, the same problem occur. thanks

    Saturday, November 20, 2010 3:18 AM
  • User1949565384 posted

    While passing this parameter to the code behind you might have put SqlParameter right? Can you please check datatype over there.

    else just try to put debug point on this line

    string str = "28538.38";
    Decimal dc = Convert.ToDecimal(str);

    This is working fine on my sample, there might be issue while you are passing to database.

    Saturday, November 20, 2010 3:38 AM
  • User759660354 posted

    Single.Parse("-0.136222", CultureInfo.InvariantCulture);
     

    Saturday, November 20, 2010 12:01 PM
  • User-1746179790 posted

    I try to debug, but the problem remain, the value in table is 2853838, my code are below:

                          

                           string SaleRate = "28538.38";
                           Decimal dc = Convert.ToDecimal(SaleRate);
                          
                          // ExchangeRate.InsertExchangeRate(Titleex[i], Convert.ToDecimal(Buyrateex[i]),Convert.ToDecimal(SaleRate));

                         
                          // SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
                           String sqlstr = String.Format(@"insert into ibss_ExchangeRate(AddedDate,AddedBy,Title,BuyRate,SaleRate)values('2010/11/21','admin','USD',123456," + dc + ")");
                        

                           using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString))
                           {
                               SqlCommand command = new SqlCommand(sqlstr, cn);
                               command.Connection.Open();
                               command.ExecuteNonQuery();
                           }

     

    help more...

     

    thanks,

    Saturday, November 20, 2010 10:15 PM
  • User759660354 posted

    ok try to change the string  "28538.38" to "28538,38" (Comma instead of dot);
    run your program again and see if the value gets correct.

    Check if the decimal value is correct.

    And if your store it in a database check if the database value gets correct.

    If yes your problem is definitly a localization problem (Either client webserver or db-server related).


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 21, 2010 2:48 PM