C # automatically convert the input number to scientific notation

Source: Internet
Author: User

A friend wrote an algorithm that converts the input integer or floating-point number to the scientific notation expression. After writing the algorithm, he asked me to help him see if there were any bugs or other issues that were not fully considered. I have been scared by the fact that I have not carefully read it-I have written nearly three hundred lines of code. I didn't say anything about him. I just went back to my computer and wrote a try.

 

Requirement: enter a number, which is represented by a scientific notation. there must be three valid numbers, and the power part must also be three digits. If the power is insufficient, the value of the power is zero.

 

The Code is as follows:

Public abstract class ScienceCount
{
Public static string KXJSF (double num)
{
Double bef = System. Math. Abs (num );
Int aft = 0;
While (bef> = 10 | (bef <1 & bef! = 0 ))
{
If (bef> = 10)
{
Bef = bef/10;
Aft ++;
}
Else
{
Bef = bef * 10;
Aft --;
}
}
Return string. Concat (num> = 0? "": "-", ReturnBef (bef), "E", ReturnAft (aft ));
}
/// <Summary>
/// Processing valid numbers
/// </Summary>
/// <Param name = "bef"> valid number </param>
/// <Returns> three valid numbers. If the number is insufficient, the value is set to zero. </returns>
Public static string ReturnBef (double bef)
{
If (bef. ToString ()! = Null)
{
Char [] arr = bef. ToString (). ToCharArray ();
Switch (arr. Length)
{
Case 1:
Case 2: return string. Concat (arr [0], ".", "00"); break;
Case 3: return string. Concat (arr [0] + "." + arr [2] + "0"); break;
Default: return string. Concat (arr [0] + "." + arr [2] + arr [3]); break;
}
}
Else
Return "000 ";
}
/// <Summary>
/// Power Processing
/// </Summary>
/// <Param name = "aft"> power </param>
/// <Returns> three-digit power. If the value is insufficient, the value is set to zero. </returns>
Public static string ReturnAft (int aft)
{
If (aft. ToString ()! = Null)
{
String end;
Char [] arr = System. Math. Abs (aft). ToString (). ToCharArray ();
Switch (arr. Length)
{
Case 1: end = "00" + arr [0]; break;
Case 2: end = "0" + arr [0] + arr [1]; break;
Default: end = System. Math. Abs (aft). ToString (); break;
}
Return string. Concat (aft> = 0? "+": "-", End );
}
Else
Return "+ 000 ";
}
}

 

Call code:

Static void Main ()
{
String num = "0 ";
While (num! = "#")
{
Num = Console. ReadLine ();
If (! String. IsNullOrEmpty (num. Trim ()))
Console. WriteLine (ScienceCount. KXJSF (double. Parse (num )));
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.