C#控制台基礎 無符號八進位小數轉換為十進位

來源:互聯網
上載者:User

標籤:pre   item   演算法   mat   eem   階段   convert   char   控制台   

鎮場詩:
———大夢誰覺,水月中建部落格。百千磨難,才知世事無常。
———今持佛語,技術無量願學。願盡所學,鑄一良心部落格。
——————————————————————————————————————————

 

1 code

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace ConsoleApplication15 8 { 9     class Program10     {11         static void Main(string[] args)12         {13             Console.WriteLine("your turn:");14             string value = Console.ReadLine();15 16             //分離字串,以小數點為界限17             string[] values = value.Split(new char[] { ‘.‘ }, StringSplitOptions.RemoveEmptyEntries);18 19             //儲存小數部分的字串20             string decimalsPart = values[1];21             //儲存整數部分的字串22             string integerPart = values[0];23 24             //小數部分出來的10進位數字一定是double類型的25             double sumOfDecimalsPart = 0.0;26             for (int i = 0; i < decimalsPart.Length; i++)27             {28                 //decimalsPart[i]是char類型,所以要加一個tostring29                  //在演算法設計的初級階段,不要寫出一個有多個變數的長式子,30                 //要把式子的每個變數都用單獨命名,然後調試的時候可以檢測到是哪一個變數出了問題31                 //int itemp = Convert.ToInt32(decimalsPart[i].ToString());32 33                 sumOfDecimalsPart += Convert.ToDouble(Convert.ToInt32(decimalsPart[i].ToString()) * Math.Pow(8, -(i+1)));34             }35 36 37             int sumOfintegerPart = 0;38             for (int i = 0; i < integerPart.Length; i++)39             {40                 //decimalsPart[i]是char類型,所以要加一個tostring41                 //在演算法設計的初級階段,不要寫出一個有多個變數的長式子,42                 //要把式子的每個變數都用單獨命名,然後調試的時候可以檢測到是哪一個變數出了問題43                 //int itemp = Convert.ToInt32(decimalsPart[i].ToString());44 45                 sumOfintegerPart += Convert.ToInt32(Convert.ToInt32(integerPart[i].ToString()) * Math.Pow(8, i));46             }47 48             Console.Write("decimalsPart:");49             Console.WriteLine(decimalsPart);50 51             Console.Write("sumOfDecimalsPart:");52             Console.WriteLine(sumOfDecimalsPart);53 54             Console.Write("integerPart:");55             Console.WriteLine(integerPart);56 57             Console.Write("sumOfintegerPart:");58             Console.WriteLine(sumOfintegerPart);59 60             Console.Write("result:");61 62             //                                 這裡實現的是整數部分與小數部分的拼接63             Console.WriteLine(sumOfintegerPart+sumOfDecimalsPart.ToString().Substring(1));64             Console.ReadKey();65         }66     }67 }

 

2 show

 

——————————————————————————————————————————
博文的精髓,在技術部分,更在鎮場一詩。
C#是優秀的語言,值得努力學習。
如果博文的內容有可以改進的地方,甚至有錯誤的地方,請留下評論,我一定努力改正,爭取鑄成一個良心部落格。
註:此文僅作為科研學習,如果我無意中侵犯了您的權益,請務必及時告知,我會做出改正。

C#控制台基礎 無符號八進位小數轉換為十進位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.