Automatically convert the input number to scientific notation (C # implementation)

Source: Internet
Author: User

This afternoon, 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 <br/>{< br/> Public static string kxjsf (double num) <br/>{< br/> double BEF = system. math. ABS (Num); <br/> int aft = 0; <br/> while (BEF> = 10 | (BEF <1 & BEF! = 0) <br/>{< br/> If (BEF> = 10) <br/>{< br/> BEF = BEF/10; <br/> aft ++; <br/>}< br/> else <br/>{< br/> BEF = BEF * 10; <br/> aft --; <br/>}< br/> return string. concat (Num> = 0? "": "-", Returnbef (BEF), "E", returnaft (AFT )); <br/>}< br/> /// <summary> <br/> // processing valid numbers <br/> /// </Summary> <br/ >/// <Param name = "BEF"> valid number </param> <br/> /// <returns> three valid numbers, if it is not enough, add zero </returns> <br/> Public static string returnbef (double BEF) <br/>{< br/> If (BEF. tostring ()! = NULL) <br/>{< br/> char [] arr = BEF. tostring (). tochararray (); <br/> switch (ARR. length) <br/>{< br/> case 1: <br/> case 2: Return string. concat (ARR [0], ". "," 00 "); break; <br/> case 3: Return string. concat (ARR [0] + ". "+ arr [2] +" 0 "); break; <br/> default: Return string. concat (ARR [0] + ". "+ arr [2] + arr [3]); break; <br/>}< br/> else <br/> return" 000 "; <br/>}< br/> // <summary> <B R/> // power processing <br/> /// </Summary> <br/> // <Param name = "AFT"> power </Param> <br/> // <returns> three-digit power, if it is not enough, add zero </returns> <br/> Public static string returnaft (INT aft) <br/>{< br/> If (aft. tostring ()! = NULL) <br/>{< br/> string end; <br/> char [] arr = system. math. ABS (AFT ). tostring (). tochararray (); <br/> switch (ARR. length) <br/> {<br/> case 1: End = "00" + arr [0]; break; <br/> case 2: end = "0" + arr [0] + arr [1]; break; <br/> default: End = system. math. ABS (AFT ). tostring (); break; <br/>}< br/> return string. concat (AFT> = 0? "+": "-", End); <br/>}< br/> else <br/> return "+ 000 "; <br/>}< br/> this article from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/miss_my_god/archive/2009/12/10/4982055.aspx

Call code:

Static void main () <br/>{< br/> string num = "0"; <br/> while (num! = "#") <Br/>{< br/> num = console. Readline (); <br/> If (! String. isnullorempty (Num. trim () <br/> console. writeline (sciencecount. kxjsf (double. parse (Num); <br/>}< br/>

The code can be run and basically meets the requirements (the input characters are not considered). If there are any deficiencies, please kindly advise.

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.