您的位置:首页 > 编程语言 > C#

ToString()的用法 C# 数学函数库

2016-07-26 15:58 393 查看
ToString()可空参数单独使用,同时可以加一个格式化参数,具体方式如下:

1. 取中文日期显示_年月 currentTime.ToString("y"); 格式:2007年1月

2. 取中文日期显示_月日 currentTime.ToString("m"); 格式:1月30日

3. 取日期显示_年月日 currentTime.ToString("d"); 格式:2007-1-30

4. 取日期显示_时分 currentTime.ToString("t"); 格式:15:35

5. Int32.Parse(变量) Int32.Parse("常量") 字符型转换 转为32位数字型

6. 变量.ToString() 字符型转换 转为字符串

   12345.ToString("n"); //生成 12,345.00

   12345.ToString("C"); //生成 ¥12,345.00

   12345.ToString("e"); //生成 1.234500e+004

   12345.ToString("f4"); //生成 12345.0000

   12345.ToString("x"); //生成 3039

7. 变量.ToString("yyyyMMdd") ; 格式:20070101

8.变量.ToString(".00") ; 格式:*.??

DateTime.Now.ToString 中的参数问题

DateTime.Now.ToString根据参数不同输出不同内容,功能很强的,总结一下。

[C#]

using System;

using System.Globalization;

public class MainClass {

   public static void Main(string[] args)  {

       DateTime dt = DateTime.Now;

       String[] format = {

           "d", "D",

           "f", "F",

           "g", "G",

           "m",

           "r",

           "s",

           "t", "T",

           "u", "U",

           "y",

           "dddd, MMMM dd yyyy",

           "ddd, MMM d "'"yy",

           "dddd, MMMM dd",

           "M/yy",

           "dd-MM-yy",

       };

       String date;

       for (int i = 0; i < format.Length; i++) {

           date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);

           Console.WriteLine(String.Concat(format[i], " :" , date));

       }

   }

}

格式字符 关联属性/说明

d ShortDatePattern

D LongDatePattern

f 完整日期和时间(长日期和短时间)

F FullDateTimePattern(长日期和长时间)

g 常规(短日期和短时间)

G 常规(短日期和长时间)

m、M MonthDayPattern

r、R RFC1123Pattern

s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)

t ShortTimePattern

T LongTimePattern

u UniversalSortableDateTimePattern 用于显示通用时间的格式

U 使用通用时间的完整日期和时间(长日期和长时间)

y、Y YearMonthPattern

 

ToString()方法是最经常使用,也需要使用的方法,可是不少人都只用到简单的无参数的方法,而实际上ToString()有4个重载方法,你转换成不同样式的方法,他们都一个一个参数那就是格式参数,以显示成你需要的格式。如下,可以转换成7种格式的:



货币 

2.5.ToString("C")

 

¥2.50

 



十进制数 

25.ToString("D5")

 

00025

 

E

科学型

25000.ToString("E")

 

2.500000E+005

 

F

固定点

25.ToString("F2")

 

25.00

 

G

常规

2.5.ToString("G")

 

2.5

 

N

数字

 

2500000.ToString("N")

 

2,500,000.00

 



十六进制

 

255.ToString("X")

FF

    formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看)

 

必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。

常用格式举例:

(1) int i=12345;

this.textBox1.Text=i.ToString();

//结果 12345(this指当前对象,或叫当前类的实例)

this.textBox2.Text=i.ToString("d8");

//结果 00012345

(2) int i=123;

double j=123.45;

string s1=string.Format("the value is {0,7:d}",i);

string s2=string.Format("the value is {0,7:f3}",j);

this.textBox1.Text=s1 ;

//结果 the value is 123

this.textBox2.Text=s2;

//结果 the value is 123.450

(3)double i=12345.6789;

this.textBox1.Text=i.ToString("f2"); //结果 12345.68

this.textBox2.Text=i.ToString("f6");

//结果 12345.678900

(4)double i=12345.6789;

this.textBox1.Text=i.ToString("n"); //结果 12,345.68

this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789

(5)double i=0.126;

string s=string.Format("the value is {0:p}",i);

this.textBox1.Text=i.ToString("p"); //结果 12.6%

this.textBox2.Text=s; //结果 the value is 12.6%

(6) DateTime dt =new DateTime(2003,5,25);

this.textBox1.Text=dt.ToString("yy.M.d");

//结果 03.5.25

this.textBox2.Text=dt.ToString(“yyyy年M月”);

//结果 2003年5月

(7) int i=123;

double j=123.45;

string s=string.Format("i:{0,-7},j:{1,7}",i,j);

//-7表示左对齐,占7位

this.textBox1.Text=s ;

//结果i:123 ,j: 123.45

_____------------_____________________————————————————————————————————————————————

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            double a = 2;

            double b=System.Math.Pow(a,3);

            Console.WriteLine(b);

            Console.ReadKey();

        }

    }

}

结果如图:



 专有的平方根函数Math.Sqrt(a)也可由Math.Pow(a,0.5)代替。

小注:要运用C#的数学函数库,直接Math点即可,不要去添加什么引用。

Math库包含的函数如下:
函   数   原   型
功     能
返  回  值
说     明
int Abs(int x)
求整数x的绝对值
绝对值
 
double Acos(double x)
计算arccos(x)的值
计算结果
-1≤x≤1
double Asin(double x)
计算arcsin(x)的值
计算结果
-1≤x≤1
double Atan(double x)
计算arctan(x)的值
计算结果
 
double atan2(double y, double x);
计算arctan(y/x)的值
计算结果
 
long BigMul(int x, int y)
计算x*y的值
计算结果
 
int Ceiling(double x)
返回大于或等于所给数字表达式x的最小整数
最小整数
 
double Cos(double x)
计算cos(x)的值
计算结果
x的单位为弧度
double Cosh(double x)
计算x的双曲余弦cosh(x)的值
计算结果
 
int DivRem(int x,int y,int z)
计算x与y的商,并将余数作为输出参数进行传递
x与y的商,z为余数
 
double Exp(double x)
求ex的值
计算结果
 
int Floor (double x)
返回小于或等于所给数字表达式x的最大整数
最大整数
 
int IEEERemainder(int x, int y)
返回x/y的余数
计算结果
 
double Log(double x)
计算ln(x)的值
计算结果
 
double Log10(double x)
计算log10(x)的值
计算结果
 
double Max(double x, double y)
返回x,y中的较大者
计算结果
 
double Min(double x, double y)
返回x,y中的较小者
计算结果
 
double Pow(double x,double y)
求xy的值
计算结果
 
int  Round(double x)
将x四舍五入到最接近的整数
计算结果
 
double  Round(double x,int y)
将x四舍五入到由y指定的小数位数
计算结果
 
int Sign(double x)
返回表示x符号的值
数值x大于0,返回1;数值x等于0返回0;数值x小于0,返回-1
 
double Sin(double x)
计算sin(x)的值
计算结果
x的单位为弧度
double Sinh(double x)
计算x的双曲正弦sinh(x)的值
计算结果
 
double Sqrt(double x)
求的值
计算结果
x≥0
double Tan(double x)
计算tan(x)的值
计算结果
x的单位为弧度
double Tanh(double x)
计算x的双曲正切tanh(x)的值
计算结果
 
这些数学函数的基本使用方法在表1中均有说明,都是静态函数,调用的时候用算术类直接调用,例如:

double d=System.Math.Sin(123.0);

对于个别函数的计算结果要注意。

例如,对于数字12.9273,Ceiling(12.9273)将返回 13,Floor(12.9273)将返回 12。

statusBarXY.Text = string.Format("{0}°{1}′{2}″西,{3}°{4}′{5}″北", e.mapX.ToString("###"),

               ((e.mapX - System.Math.Floor(e.mapX)) * 60).ToString("##"),

               ((((e.mapX - System.Math.Floor(e.mapX)) * 60) - System.Math.Floor(((e.mapX - System.Math.Floor(e.mapX)) * 60))) * 60).ToString("##"),

               e.mapY.ToString("###"),

               ((e.mapY - System.Math.Floor(e.mapY)) * 60).ToString("##"),

               ((((e.mapY - System.Math.Floor(e.mapY)) * 60) - System.Math.Floor(((e.mapY - System.Math.Floor(e.mapY)) * 60))) * 60).ToString("##"));

                //MapCtrl.MapUnits.ToString().Substring(4));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: