Question

****UPDATED****

How to convert an exponent and coefficient to an integer? Is there a built-in method in SQL?

This is the value in scientific notation 6,1057747657e+011

Était-ce utile?

La solution

DECLARE @s VARCHAR(25);
DECLARE @i BIGINT;

SET @s = '6.1057747657e+011';
SET @i = CAST(@s as FLOAT(53));

SELECT @i;

Results 610577476570

You need to store the result as a BIGINT because the number is too large for a 32-bit INT. Note that an implicit conversion is being done from FLOAT(53) to BIGINT.

If you want to control the rounding, you can use the ROUND(), FLOOR() or CEILING() functions. For example:

SET @i = ROUND(CAST(@s as FLOAT(53)), -2);

If it is possible that the input string might contain an invalid number, you would need to add error handling.

DECLARE @s VARCHAR(25);
DECLARE @i BIGINT;

SET @s = 'rubbish';

BEGIN TRY
    SET @i = CAST(@s as FLOAT(53));
    SELECT @i;
END TRY
BEGIN CATCH
    -- error handling goes here
END CATCH

(Tested using T-SQL on SQL Server 2012.)

Autres conseils

I think like this but to make into a integer I guess use BigInteger? let me research more

   try {
      Console.WriteLine(Double.Parse("6.1057747657e+011"));
   }   
   catch (OverflowException) {
      Console.WriteLine("{0} is outside the range of the Double type.",
                        value);
   }

Different answer below.

http://msdn.microsoft.com/en-us/library/dd268285(v=vs.110).aspx

The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. The value parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.

BigInteger value = BigInteger.Parse("-903145792771643190182");
string[] specifiers = { "C", "D", "D25", "E", "E4", "e8", "F0", 
                        "G", "N0", "P", "R", "X", "0,0.000", 
                        "#,#.00#;(#,#.00#)" };

foreach (string specifier in specifiers)
   Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));

// The example displays the following output: 
//       C: ($903,145,792,771,643,190,182.00) 
//       D: -903145792771643190182 
//       D25: -0000903145792771643190182 
//       E: -9.031458E+020 
//       E4: -9.0315E+020 
//       e8: -9.03145793e+020 
//       F0: -903145792771643190182 
//       G: -903145792771643190182 
//       N0: -903,145,792,771,643,190,182 
//       P: -90,314,579,277,164,319,018,200.00 % 
//       R: -903145792771643190182 
//       X: CF0A55968BB1A7545A 
//       0,0.000: -903,145,792,771,643,190,182.000 
//       #,#.00#;(#,#.00#): (903,145,792,771,643,190,182.00)     

In your case you might do

BigInteger value = System.Numerics.BigInteger.Parse("6.1057747657e+011", NumberStyles.Float, CultureInfo.InvariantCulture);

You must have .NET Framework 4.5 or higher for this to work.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top