locked
How to store exponential value in sql server RRS feed

  • Question

  • User-1569093213 posted

    I want to store exponential value as it is in database in sql server.How can I store exponential value in sql server as it is without any formatting?

    Please help me out with same.

    Thanking you.

    Thursday, May 21, 2020 11:08 AM

Answers

  • User475983607 posted

    I'm very confused by your response.  The string format provided works exactly as you requested.

    static void Main(string[] args)
    {
        decimal num = 12300000000m;
        Console.WriteLine(string.Format("{0:0.##E+00}", num));
    }

    Result

    1.23E+10

    Store the numeric actual number value (12300000000) in the database.  Format the value when you are displaying the value page as shown above.

    If you need to convert the string 1.23+E10 to a numeric value then use standard .NET parsing which is openly published.

    static void Main(string[] args)
    {
        decimal num = 12300000000m;
        string numExp = string.Format("{0:0.##E+00}", num);
        Console.WriteLine(numExp);
    
        decimal num2 = Decimal.Parse(numExp, System.Globalization.NumberStyles.Float);
        Console.WriteLine(num2);
    
    }
    1.23E+10
    12300000000

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 23, 2020 5:14 PM

All replies

  • User475983607 posted

    I want to store exponential value as it is in database in sql server.How can I store exponential value in sql server as it is without any formatting?

    The problem you are trying to solve is not clear.

    An exponent is just a symbol for a number.  Exponential is used to describe a function.  If you are trying to store a numeric value then pick a column type that can store the value.  If you are trying to store the mathematical function then use a VARCHAR. 

    Thursday, May 21, 2020 11:19 AM
  • User-474980206 posted

    use the data type float:

    create table foo (
        mycolumn float not null
    ) 
    

    note: a sqlserver float is a C# double (use real for single)

    Thursday, May 21, 2020 3:21 PM
  • User711641945 posted

    Hi bhushan_microsoft,

    As megbhard said,Exponential is just used to describe a function.You could try like below:

    var value = 12345.6789;
    var exponential = value.ToString("e", CultureInfo.InvariantCulture);
    //then store the exponential to the database and the column's type is like nvarchar in database

    Best Regards,

    Rena

    Friday, May 22, 2020 8:31 AM
  • User-1569093213 posted

    Hello ,

    I have textbox from which I am accepting exponential value from user and I am trying to store it into database.the datatype for storing the exponential value into database is 
    "FLOAT".let say I am storing 1.23E+10 value into database then it is getting storied as 123000000 into database. but the actual value is 1.23E+10.so when I fetch the value from database for update, it is coming like 12300000 and not like original one (i.e.1.23E+10).So how can I handle the situation and how I can get the same value (i.e.1.23E+10) from database for update.

    Thanking you.

    Please suggest me some solution.

    Saturday, May 23, 2020 12:59 PM
  • User1034446946 posted

    https://www.c-sharpcorner.com/UploadFile/c25b6d/exponential-e-format-specifier-in-C-Sharp1/

    Saturday, May 23, 2020 1:46 PM
  • User475983607 posted

    Mathematically 123000000 is 1.23+E. There is not technical problem with the code.  

    As explained above, if you want to display the value as an exponent on the screen then use the standard .NET numeric formatting.

    string.Format("{0:0.##E+00}", theValue);

    Reference documentation.

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

    Saturday, May 23, 2020 1:50 PM
  • User-1569093213 posted

    Hello,

    If I use the given string.Format("{0:0.##E+00}", theValue); code while fetching the value from database in my application and then if I store 1.23E+10 value then in database it is getting stored as 12300000000 and when I retrieve it from database it is formatted like 1.230000E+010 and not as entered(i.e.original) 1.23E+10.

    How can I get that entered value is 1.23E+10. and i need to format database value (12300000000 ) as 1.23E+10. while showing it on UI for updating it?

    So how can I get the same value while saving and while fetching it from database(i.e.1.23E+10) both the times?

    Please help for formatting the exponential value.

    Thanking you.

    Saturday, May 23, 2020 2:59 PM
  • User475983607 posted

    I'm very confused by your response.  The string format provided works exactly as you requested.

    static void Main(string[] args)
    {
        decimal num = 12300000000m;
        Console.WriteLine(string.Format("{0:0.##E+00}", num));
    }

    Result

    1.23E+10

    Store the numeric actual number value (12300000000) in the database.  Format the value when you are displaying the value page as shown above.

    If you need to convert the string 1.23+E10 to a numeric value then use standard .NET parsing which is openly published.

    static void Main(string[] args)
    {
        decimal num = 12300000000m;
        string numExp = string.Format("{0:0.##E+00}", num);
        Console.WriteLine(numExp);
    
        decimal num2 = Decimal.Parse(numExp, System.Globalization.NumberStyles.Float);
        Console.WriteLine(num2);
    
    }
    1.23E+10
    12300000000

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 23, 2020 5:14 PM
  • User-1569093213 posted

    Hello,

    Thanks for your quick reply...thank you so much...

    Actually it was my mistake...I was doing like below

    Console.WriteLine(string.Format("{0:0.##E+00}", num!=0? num.ToString("E",somecultureInfo):"someValue");

    Because of that it was showing some wrong output.. but you saved me...thanks so much..

    Will get back if some formatting is still required..

    Thanks

    Saturday, May 23, 2020 5:56 PM