コード例 #1
0
ファイル: MathV.cs プロジェクト: YanjieHe/VISAP2.0_Advanced
        public static string NumberPolish(string Number)
        {
            //首先要判别输入的数字是否为科学计数法
            int Char_E = Number.IndexOf('E');

            if (Char_E != -1)
            {
                //如果是科学计数法则先转化为常规格式
                Number = (new BigNumber(Number.Substring(0, Char_E - 1))
                          * MathV.Pow(10, Number.Substring(Char_E + 1,
                                                           Number.Length - Char_E - 1))).ToString();
            }
            //目标为控制位数在10位
            Number = Number.Trim();
            int        CountDecimalPoint = 0;
            int        ScientificNumber;
            string     ScientificNotation = "";
            int        digits_part;
            BigDecimal NumberProcess = 0;
            string     ProcessedNumber;

            char[] separator = { '.' };
            //用于分割小数点
            string[] NumberParts = new string[2];
            int      IsNegative  = 0;
            //用来判定数字是否为负
            int zero_count = 0;

            //用于计算小数点后有多少个零
            NumberParts = Number.Split(separator);
            if (NumberParts[0].Length + 5 > 10)
            //四位小数加上1个小数点
            {
                if (Number[0] == '-')
                {
                    IsNegative = 1;
                }
                foreach (char FindDecimalPoint in Number)
                {
                    if (FindDecimalPoint == '.')
                    {
                        CountDecimalPoint = 1;
                        break;
                    }
                }
                if (CountDecimalPoint == 0)
                {
                    //无小数点则只有整数部分
                    ScientificNumber = Number.Length - IsNegative - 1;
                    //转换为科学计数法
                    if (ScientificNumber <= 9 && ScientificNumber > 0)
                    {
                        ScientificNotation = "0" + ScientificNumber.ToString();
                        //如果科学计数法为个位要补零
                    }
                    else
                    {
                        ScientificNotation = ScientificNumber.ToString();
                    }
                    //数字正负无需考虑
                    if (ScientificNotation.Length + 1 + 2 <= 9)
                    {
                        //例如:-3.1415E+05
                        //数字负号占一位,E和科学计数法的符号占两位
                        //大于9的情况不做考虑,数字太大。
                        digits_part = 9 - (ScientificNotation.Length + 1 + 2);
                        //小数位由此算出
                        NumberProcess   = new BigDecimal(Number.ToString()) / Math.Pow(10, ScientificNumber);
                        ProcessedNumber = MathV.round(NumberProcess.ToString(), digits_part, 0) + "E+" + ScientificNotation;
                    }
                    else
                    {
                        //如果已经大于已经大于九位了,则不作处理
                        //这种情况极为罕见,不考虑
                        NumberProcess   = new BigDecimal(Number.ToString()) / Math.Pow(10, ScientificNumber);
                        ProcessedNumber = MathV.round(NumberProcess.ToString(), 0, 0) + "E+" + ScientificNotation;
                    }
                }
                else
                {
                    //有小数点的情况
                    NumberParts = Number.Split(separator);
                    if (NumberParts[0] != "0")
                    {
                        if (NumberParts[0].Length + 1 + 4 >= 9)
                        {
                            //如果保留四位小数之后依旧总长度超过九,则进行如下操作
                            //判断整数部分是否为0
                            //如果不为0,继续科学计数法的处理,舍去小数部分
                            ScientificNumber = NumberParts[0].Length - IsNegative - 1;
                            //转换为科学计数法
                            if (ScientificNumber <= 9 && ScientificNumber > 0)
                            {
                                ScientificNotation = "0" + ScientificNumber.ToString();
                                //如果科学计数法为个位要补零
                            }
                            else
                            {
                                ScientificNotation = ScientificNumber.ToString();
                            }
                            //数字正负无需考虑
                            if (ScientificNotation.Length + 1 + 2 <= 9)
                            {
                                //例如:-3.1415E+05
                                //数字负号占一位,E和科学计数法的符号占两位
                                //大于9的情况不做考虑,数字太大。
                                digits_part = 9 - (ScientificNotation.Length + 1 + 2);
                                //小数位由此算出
                                NumberProcess   = new BigDecimal(Number.ToString()) / Math.Pow(10, ScientificNumber);
                                ProcessedNumber = MathV.round(NumberProcess.ToString(), digits_part, 0) + "E+" + ScientificNotation;
                            }
                            else
                            {
                                //如果已经大于已经大于九位了,则不作处理
                                //这种情况极为罕见,不考虑
                                NumberProcess   = new BigDecimal(Number.ToString()) / Math.Pow(10, ScientificNumber);
                                ProcessedNumber = MathV.round(NumberProcess.ToString(), 0, 0) + "E+" + ScientificNotation;
                            }
                        }
                        else
                        {
                            ProcessedNumber = MathV.round(Number, 4, 0);
                        }
                    }
                    else
                    {
                        zero_count = 0;
                        foreach (char EachDigit in NumberParts[1])
                        {
                            if (EachDigit == '0')
                            {
                                zero_count++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        ScientificNumber = zero_count + 1;
                        //科学计数法的数字为零的个数加1
                        if (ScientificNumber <= 9 && ScientificNumber > 0)
                        {
                            ScientificNotation = "0" + ScientificNumber.ToString();
                            //如果科学计数法为个位要补零
                        }
                        else
                        {
                            ScientificNotation = ScientificNumber.ToString();
                        }
                        if (ScientificNotation.Length + 1 + 2 <= 9)
                        {
                            //例如:-2.654E-05
                            //数字负号占一位,E和科学计数法的符号占两位
                            //大于9的情况不做考虑,数字太大。
                            digits_part = 9 - (ScientificNotation.Length + 1 + 2);
                            //小数位由此算出
                            NumberProcess   = new BigDecimal(Number.ToString()) * Math.Pow(10, ScientificNumber);
                            ProcessedNumber = MathV.round(NumberProcess.ToString(), digits_part, 0) + "E-" + ScientificNotation;
                        }
                        else
                        {
                            //如果已经大于已经大于九位了,则不作处理
                            //这种情况极为罕见,不考虑
                            NumberProcess   = new BigDecimal(Number.ToString()) * Math.Pow(10, ScientificNumber);
                            ProcessedNumber = MathV.round(NumberProcess.ToString(), 0, 0) + "E-" + ScientificNotation;
                        }
                    }
                }
            }
            else
            {
                ProcessedNumber = MathV.round(Number, 4, 0);
                //如果本身长度就在十个单位以内,则不作处理
                //保留四位小数
            }
            if (ProcessedNumber.Length < 11)
            {
                ProcessedNumber = ProcessedNumber.PadLeft(11, ' ');
                //11个单位的长度是为了预留量,以免出现连续进位的情况
            }
            return(ProcessedNumber);
        }
コード例 #2
0
ファイル: SpatialMap.cs プロジェクト: chenggang0815/VISAP
        private void button1_Click(object sender, EventArgs e)
        {
            int ProvinceNum = 0;
            int VariableNum = 0;

            if (Int32.TryParse(texbox_province.Text, out ProvinceNum))
            {
                if (Int32.TryParse(textBox_variable.Text, out VariableNum))
                {
                    string[] Province_name = Tabulation.ReadVector(MainForm.MainDT, ProvinceNum - 1).ToArray();
                    string[] Variable_data = Tabulation.ReadVector(MainForm.MainDT, VariableNum - 1).ToArray();
                    double[] data1         = new double[31];

                    string[] match = new string[] { "黑", "内", "新", "吉", "辽", "甘", "河北", "京", "山西", "津", "陕", "夏", "青", "山东", "藏", "河南", "苏", "安", "川", "湖北", "重", "上", "浙", "湖南", "江西", "云", "贵", "福", "广西", "海南", "广东" };
                    for (int i = 0; i < 31; i++)
                    {
                        for (int j = 0; j < 31; j++)
                        {
                            if (Province_name[j].IndexOf(match[i]) > -1)
                            {
                                data1[i] = Convert.ToDouble(Variable_data[j]);

                                break;
                            }
                        }
                    }
                    string    str2       = Environment.CurrentDirectory;
                    DataTable dt         = new DataTable();
                    DataTable dt_fill    = new DataTable();
                    string    coor_path  = str2 + "\\coordinates_tiny.csv";
                    string    coor_fill  = str2 + "\\coordinates_tinyfill.csv";
                    char[]    separators = new char[1];
                    separators[0] = ',';
                    dt            = Tabulation.LoadFromCSVFile(coor_path, separators);
                    dt_fill       = Tabulation.LoadFromCSVFile(coor_fill, separators);
                    int[] X_fill    = new int[dt_fill.Rows.Count];
                    int[] Y_fill    = new int[dt_fill.Rows.Count];
                    int[] Num_count = new int[31]; //31个省的填充坐标点数
                    Num_count[0]  = 5436;
                    Num_count[1]  = 18313;
                    Num_count[2]  = 35839;
                    Num_count[3]  = 37966;
                    Num_count[4]  = 39530;
                    Num_count[5]  = 43696;
                    Num_count[6]  = 45647;
                    Num_count[7]  = 45814;
                    Num_count[8]  = 47410;
                    Num_count[9]  = 47531;
                    Num_count[10] = 49571;
                    Num_count[11] = 50103;
                    Num_count[12] = 57224;
                    Num_count[13] = 58756;
                    Num_count[14] = 70210;
                    Num_count[15] = 71816;
                    Num_count[16] = 72785;
                    Num_count[17] = 74121;
                    Num_count[18] = 78660;
                    Num_count[19] = 80410;
                    Num_count[20] = 81181;
                    Num_count[21] = 81229;
                    Num_count[22] = 82155;
                    Num_count[23] = 84079;
                    Num_count[24] = 85602;
                    Num_count[25] = 89021;
                    Num_count[26] = 90616;
                    Num_count[27] = 91701;
                    Num_count[28] = 93791;
                    Num_count[29] = 94079;
                    Num_count[30] = 95620;

                    for (int i = 0; i < dt_fill.Rows.Count; i++)
                    {
                        //读取每个省的填充坐标数据
                        for (int j = 0; j < 31; j++)
                        {
                            if (j == 0)
                            {
                                if (i < Num_count[j])
                                {
                                    X_fill[i] = Convert.ToInt32(dt_fill.Rows[i][0]); // 经度
                                    Y_fill[i] = Convert.ToInt32(dt_fill.Rows[i][1]); //纬度
                                }
                            }
                            else
                            {
                                if (i < Num_count[j] && i >= Num_count[j - 1])
                                {
                                    X_fill[i] = Convert.ToInt32(dt_fill.Rows[i][0]); // 经度
                                    Y_fill[i] = Convert.ToInt32(dt_fill.Rows[i][1]); //纬度
                                    //ID_coordinates[i] = Convert.ToDouble(dt.Rows[i][0]);
                                }
                            }
                        }
                    }
                    //double[] data1 = new double[] {0.589967025780379,2.76640767785393,2.94486656044847,5.18774795087285,9.55568524078571,7.21217446706751,4.81608668665851,5.67326916712699,9.54321203609774,6.27015178543689,0.354135803190215,8.14711495872654,2.47480472823066,3.05263290782758,2.21504588700546,6.70140475318904,6.05666406518314,2.40509705776911,2.60405493113485,1.6725398082315,5.49355137602265,2.82091154095248,9.6588283411846,5.38501889047855,2.50490878854465,0.604765223162727,1.58887244642718,1.17203933984219,2.20719788025169,2.15731627156846,9.43471799430495}; //按照黑龙江 。。。。 的顺序排好的数
                    double[] data = new double[31];
                    data1.CopyTo(data, 0);

                    double[] datarank = MathV.Sort(31, data1);//从小到大排序
                    this.chart1.Series.Clear();
                    int      count_class = 4;
                    double[] quan_value  = new double[count_class + 1];
                    quan_value[0] = datarank[0];
                    quan_value[1] = datarank[7];
                    quan_value[2] = datarank[15];
                    quan_value[3] = datarank[23];
                    quan_value[4] = datarank[30];
                    for (int i = 0; i < count_class; i++)
                    {
                        Series series = new Series();
                        series.ChartType = SeriesChartType.FastPoint;
                        this.chart1.Series.Add(series);
                    }
                    for (int i = 0; i < 4; i++)
                    {
                        Series series = new Series();
                        series.Name      = "(" + MathV.round(quan_value[i].ToString(), 2, 0) + "," + MathV.round(quan_value[i + 1].ToString(), 2, 0) + ")";
                        series.ChartType = SeriesChartType.Column;
                        this.chart1.Series.Add(series);
                    }
                    //台湾不能忘
                    Series series_taiwan = new Series();
                    series_taiwan.ChartType = SeriesChartType.FastPoint;   //边界
                    this.chart1.Series.Add(series_taiwan);
                    series_taiwan.MarkerStyle       = MarkerStyle.Circle;
                    series_taiwan.MarkerSize        = 2;
                    series_taiwan.IsVisibleInLegend = false;
                    series_taiwan.Color             = Color.FromArgb(6, 52, 156);
                    if (comboBox_theme.Text == "蓝色")
                    {
                        this.chart1.Series[0].Color = Color.FromArgb(151, 182, 251);
                        this.chart1.Series[1].Color = Color.FromArgb(78, 131, 248);
                        this.chart1.Series[2].Color = Color.FromArgb(9, 71, 219);
                        this.chart1.Series[3].Color = Color.FromArgb(6, 52, 156);
                        this.chart1.Series[4].Color = Color.FromArgb(151, 182, 251);
                        this.chart1.Series[5].Color = Color.FromArgb(78, 131, 248);
                        this.chart1.Series[6].Color = Color.FromArgb(9, 71, 219);
                        this.chart1.Series[7].Color = Color.FromArgb(6, 52, 156);

                        series_taiwan.Color = Color.FromArgb(6, 52, 156);
                    }
                    else if (comboBox_theme.Text == "青色")
                    {
                        this.chart1.Series[0].Color = Color.FromArgb(128, 234, 248);
                        this.chart1.Series[1].Color = Color.FromArgb(34, 217, 242);
                        this.chart1.Series[2].Color = Color.FromArgb(12, 190, 214);
                        this.chart1.Series[3].Color = Color.FromArgb(8, 119, 134);
                        this.chart1.Series[4].Color = Color.FromArgb(128, 234, 248);
                        this.chart1.Series[5].Color = Color.FromArgb(34, 217, 242);
                        this.chart1.Series[6].Color = Color.FromArgb(12, 190, 214);
                        this.chart1.Series[7].Color = Color.FromArgb(8, 119, 134);

                        series_taiwan.Color = Color.FromArgb(8, 119, 134);
                    }
                    else if (comboBox_theme.Text == "绿色")
                    {
                        this.chart1.Series[0].Color = Color.FromArgb(122, 250, 146);
                        this.chart1.Series[1].Color = Color.FromArgb(55, 247, 92);
                        this.chart1.Series[2].Color = Color.FromArgb(9, 221, 49);
                        this.chart1.Series[3].Color = Color.FromArgb(6, 144, 32);
                        this.chart1.Series[4].Color = Color.FromArgb(122, 250, 146);
                        this.chart1.Series[5].Color = Color.FromArgb(55, 247, 92);
                        this.chart1.Series[6].Color = Color.FromArgb(9, 221, 49);
                        this.chart1.Series[7].Color = Color.FromArgb(6, 144, 32);

                        series_taiwan.Color = Color.FromArgb(6, 144, 32);
                    }
                    else if (comboBox_theme.Text == "红色")
                    {
                        this.chart1.Series[0].Color = Color.FromArgb(253, 149, 141);
                        this.chart1.Series[1].Color = Color.FromArgb(251, 73, 59);
                        this.chart1.Series[2].Color = Color.FromArgb(242, 21, 4);
                        this.chart1.Series[3].Color = Color.FromArgb(196, 0, 0);
                        this.chart1.Series[4].Color = Color.FromArgb(253, 149, 141);
                        this.chart1.Series[5].Color = Color.FromArgb(251, 73, 59);
                        this.chart1.Series[6].Color = Color.FromArgb(242, 21, 4);
                        this.chart1.Series[7].Color = Color.FromArgb(196, 0, 0);

                        series_taiwan.Color = Color.FromArgb(196, 0, 0);
                    }

                    for (int i = 0; i < 4; i++)
                    {
                        this.chart1.Series[i].MarkerStyle       = MarkerStyle.Circle;
                        this.chart1.Series[i].MarkerSize        = 2;
                        this.chart1.Series[i].IsVisibleInLegend = false;
                    }

                    Series series5 = new Series();
                    series5.ChartType = SeriesChartType.FastPoint;   //边界
                    this.chart1.Series.Add(series5);
                    series5.MarkerStyle       = MarkerStyle.Square;
                    series5.MarkerSize        = 2;
                    series5.IsVisibleInLegend = false;
                    if (comboBox_boundary.Text == "无边界")
                    {
                    }
                    else if (comboBox_boundary.Text == "白色")
                    {
                        series5.Color = Color.White;
                        for (int i = 0; i < dt.Rows.Count - 168; i++)
                        {
                            series5.Points.AddXY(Convert.ToInt32(dt.Rows[i][1]), Convert.ToInt32(dt.Rows[i][2]));
                        }
                    }
                    else
                    {
                        series5.Color = Color.Black;
                        for (int i = 0; i < dt.Rows.Count - 168; i++)
                        {
                            series5.Points.AddXY(Convert.ToInt32(dt.Rows[i][1]), Convert.ToInt32(dt.Rows[i][2]));
                        }
                    }

                    for (int i = 12504; i < 12672; i++)
                    {
                        series_taiwan.Points.AddXY(Convert.ToInt32(dt.Rows[i][1]), Convert.ToInt32(dt.Rows[i][2]));
                    }

                    for (int i = 0; i < 31; i++)
                    {
                        if (data[i] <= quan_value[1])
                        {
                            if (i == 0)
                            {
                                for (int j = 0; j < Num_count[i]; j++)
                                {
                                    //MessageBox.Show(X_fill[j].ToString());
                                    this.chart1.Series[0].Points.AddXY(X_fill[j], Y_fill[j]);  //添加对应数据点
                                }
                            }
                            else
                            {
                                for (int j = Num_count[i - 1]; j < Num_count[i]; j++)
                                {
                                    this.chart1.Series[0].Points.AddXY(X_fill[j], Y_fill[j]);  //添加对应数据点
                                }
                            }
                        }
                        if (data[i] > quan_value[1] && data[i] <= quan_value[2])
                        {
                            if (i == 0)
                            {
                                for (int j = 0; j < Num_count[i]; j++)
                                {
                                    this.chart1.Series[1].Points.AddXY(X_fill[j], Y_fill[j]);  //添加对应数据点
                                }
                            }
                            else
                            {
                                for (int j = Num_count[i - 1]; j < Num_count[i]; j++)
                                {
                                    this.chart1.Series[1].Points.AddXY(X_fill[j], Y_fill[j]);  //添加对应数据点
                                }
                            }
                        }
                        if (data[i] > quan_value[2] && data[i] <= quan_value[3])
                        {
                            if (i == 0)
                            {
                                for (int j = 0; j < Num_count[i]; j++)
                                {
                                    this.chart1.Series[2].Points.AddXY(X_fill[j], Y_fill[j]);  //添加对应数据点
                                }
                            }
                            else
                            {
                                for (int j = Num_count[i - 1]; j < Num_count[i]; j++)
                                {
                                    this.chart1.Series[2].Points.AddXY(X_fill[j], Y_fill[j]);  //添加对应数据点
                                }
                            }
                        }
                        if (data[i] > quan_value[3])
                        {
                            if (i == 0)
                            {
                                for (int j = 0; j < Num_count[i]; j++)
                                {
                                    this.chart1.Series[3].Points.AddXY(X_fill[j], Y_fill[j]);  //添加对应数据点
                                }
                            }
                            else
                            {
                                for (int j = Num_count[i - 1]; j < Num_count[i]; j++)
                                {
                                    this.chart1.Series[3].Points.AddXY(X_fill[j], Y_fill[j]);  //添加对应数据点
                                }
                            }
                        }
                    }
                }
            }
        }