天天看点

浮点数取小数点后两位和保留两位(c#)

浮点数取小数点后两位(不舍入):

double d="12.345";

string str=d.ToString();

int count=str.LastIndexOf(".");

double d1=Double.Parse(str.SubString(0,count+2));

//output d1=12.34

浮点数保留小数点后两位(舍入)

double d="12.34";

double d1=Double.Parse(d.ToString("2F"));

//output d1=12.35