怎样才能把得到的小数取整数?比如,80.08取80?把小数点后面的数删除

vkv123 2012-03-30 02:15:24
哪位知道???????????、、
...全文
330 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
慕容玄明 2012-03-30
  • 打赏
  • 举报
回复
直接(int)强转,小数点后面就没了
theks 2012-03-30
  • 打赏
  • 举报
回复
如果是小数,那就用Math类的取整方法,注意它有向上取整和向下取整,负数可能会有点小问题。
如果是字符串,只是想要小数点前面的数字的话,截取字符串更方便。
darrenjj 2012-03-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

C# code

double gg=80.808;
double x=Math.Floor(gg);
[/Quote]

赞同Floor
xiaoning8201 2012-03-30
  • 打赏
  • 举报
回复
// D 10进制数
25.ToString("D5"); // 25000
// E 科学型
25000.ToString("E"); // 2.500000E+005
// F 固定点
25.ToString("F2"); // 25.00 "F?"表示保持几位小数
// G 常规
2.5.ToString("G"); // 2.5
// N 数字
2500000.ToString("N"); // 2,500,000.00
// X 16进制
255.ToString("X"); // FF
干巴猴儿 2012-03-30
  • 打赏
  • 举报
回复

double gg=80.808;
double x=Math.Floor(gg);
意飞翔心飞扬 2012-03-30
  • 打赏
  • 举报
回复

double num = 80.808;
console.WriteLine(int.Parse(num.toString().Split('.')[1]));
EnForGrass 2012-03-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用楼主 的回复:
哪位知道???????????、、

不用四舍五入吗?那你直接截取算了
[/Quote]


double gg = 80.808;
int gx=Convert.ToInt32(gg.ToString("#"));//这个是会四舍五入
int gy = Convert.ToInt32(gg.ToString().Substring(0,gg.ToString().IndexOf('.')));//直接截取
EnForGrass 2012-03-30
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
哪位知道???????????、、
[/Quote]
不用四舍五入吗?那你直接截取算了
SQL777 2012-03-30
  • 打赏
  • 举报
回复
ConvertToint
用2051芯片配合4位码管制作.以下是程序 //***********DS18B20字温度计C程序****************// //*MCU: AT892051 // //*MCU-crystal: 12M // //*Version: 01 // //*Last Updata: 2007-6-10 // //*Author: zhaojun // //*Description: // //DS18B20的读写程序,据脚P3.4 // //温度传感器18B20汇编程序,采用器件默认的12位转化 // //最大转化时间750微秒,显示温度-55到+125度,显示精度 // //为0.1度,显示采用4位LED共阳显示测温值 // //P1口为段码输入,P0~P3为位选 // /***************************************************/ #include "reg51.h" #include "intrins.h" //_nop_();延时函用 #define Disdata P1 //段码输出口 #define discan P3 //扫描口 #define uchar unsigned char #define uint unsigned int sbit DQ=P3^4; //温度输入口 sbit DIN=P1^7; //LED控制 uint h; uint temp; // // //**************温度部分用查表法***********// uchar code ditab[16]= {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09}; // uchar code dis_7[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf}; //共阳LED段码表 "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "不亮" "-" uchar code scan_con[4]={0xfe,0xfd,0xfb,0xf7}; //列扫描控制字 uchar data temp_data[2]={0x00,0x00}; //读出温度暂放 uchar data display[5]={0x00,0x00,0x00,0x00,0x00}; //显示单元据,共4个据和一个运算暂用 // // // /*****************11us延时函*************************/ // void delay(uint t) { for (;t>0;t--); } // /****************显示扫描函***************************/ scan() { char k; for(k=0;k<4;k++) //4位LED扫描控制 { Disdata=dis_7[display[k]]; //据显示 if (k==1){DIN=0;} //显示 discan=scan_con[k]; //位选 delay(200); } } // // /****************DS18B20复位函*****
#include #include #include code unsigned char seg7code[11]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92, 0x82,0xf8,0x80,0x90,0x40}; //显示段码 sbit TMDAT =P3^1; //DS18B20 的据输入/输出脚DQ,根据情况设定 sbit jia=P2^1; sbit jian=P2^0; sbit hong=P1^0; //红色警告灯 sbit sheng=P1^1; //蜂鸣器 sbit lan=P1^2; //兰色灯 bit write=0; //写24C08 的标志; j=30; unsigned int sdata;//测量到的温度的部分 unsigned char xiaoshu1;//第一位 unsigned char xiaoshu2;//第二位 unsigned char xiaoshu;//两位 bit fg=1; //温度正负标志 ////////24C08 读写驱动程序//////////////////// sbit scl=P3^4; // 24c08 SCL sbit sda=P3^5; // 24c08 SDA void delay1(unsigned char x) { unsigned int i; for(i=0;i取一个字节据///// unsigned char x24c08_read(unsigned char address) { unsigned char i; start(); writex(0xa0); clock(); writex(address); clock(); start(); writex(0xa1); clock(); i=readx(); stop(); delay1(10); return(i); } //////向24c02 的address 地址中写入一字节据info///// void x24c08_write(unsigned char address,unsigned char info) { EA=0; start(); writex(0xa0); clock(); writex(address); clock(); writex(info); clock(); stop(); EA=1; delay1(50); } /*////////////24C08 读写驱动程序完///////////////////// void Delay2(unsigned int tc) //延时程序 { while( tc != 0 ) {unsigned int i; for(i=0; i<100; i++); tc--;} } */ //////////*显示延时程序*/////////////// void Delay(unsigned int tc) {while( tc != 0 ) {unsigned int i; for(i=0; i<80; i++); tc--;} } ////////////延时部分/////////////// void yanshi (unsigned int count) { unsigned char i; while(count--) {for(i=0;i<115;i++);} } /////////////发送复位/////////////// void fashong (void) { unsigned char i; TMDAT = 0; for(i=0;i<103;i++); TMDAT = 1; for(i=0;i<4;i++); } bit tmrbit (void) //读一位// { unsigned int i; bit dat; TMDAT = 0; i++; TMDAT = 1; i++; i++; //微量延时 // dat = TMDAT; for(i=0;i<8;i++); return (dat); } unsigned char tmrbyte (void) //读一个字节 { unsigned char i,j,dat; dat = 0; for (i=1;i<=8;i++) { j = tmrbit(); dat = (j << 7) | (dat >> 1); } return (dat); } void tmwbyte (unsigned char dat) //写一个字节 { unsigned char j,i; bit testb; for (j=1;j<=8;j++) { testb = dat & 0x01; dat = dat >> 1; if (testb) { TMDAT = 0; //写0 i++; i++; TMDAT = 1; for(i=0;i<8;i++); } else { TMDAT = 0; //写0 for(i=0;i<8;i++); TMDAT = 1; i++; i++;} } } void tmstart (void) //发送ds1820 开始转换 { fashong(); //复位 yanshi(1); //延时 tmwbyte(0xcc); //跳过序列号命令 tmwbyte(0x44); //发转换命令 44H, } void tmrtemp (void) //读温度 { unsigned char a,b; fashong (); //复位 yanshi (1); //延时 tmwbyte (0xcc); //跳过序列号命令 tmwbyte (0xbe); //发送读命令 a = tmrbyte (); //读低位温度 b = tmrbyte (); //读高位温度 if(b>0x7f) //最高位为1 时温度是负 {a=~a; b=~b+1; //补码转换,反加一 fg=0; //读温度为负时fg=0 } sdata = a/16+b*16; //部分 xiaoshu1 = (a&0x0f)*10/16; //第一位 xiaoshu2 = (a&0x0f)*100/16;// xiaoshu=xiaoshu1*10+xiaoshu2; // } void DS18B20PRO(void) { tmstart(); //yanshi(5); //如果是不断地读的话可以不延 tmrtemp(); //读温度,执行完毕温度将存于 } void Led() { if(fg==1) //温度为正时显示的据 { P2=P2&0xef; P0=seg7code[sdata/10]; //输 Delay(2); P2=P2|0xf0; P2=P2&0xdf; P0=seg7code[sdata]|0x80; //输出个 Delay(2); P2=P2|0xf0; P2=P2&0xbf; P0=seg7code[xiaoshu1]; //输出 Delay(2); P2=P2|0xf0; P2=P2&0x7f; P0=seg7code[xiaoshu2]; //输出小 Delay(1); P2=P2|0xf0; Delay(2); P2=P2|0xf0;P2=P2&0xf7; P0=seg7code[j/10]; //输出十位 Delay(2); P2=P2|0x0f; P2=P2&0xfb; P0=seg7code[j]|0x80; //输出个位 Delay(1); P2=P2|0x0f; /* P2=P2&0x7f; P0=seg7code[sdata/10]; //输出十位 Delay(8); P2=P2|0xf0;P2=P2&0xbf; P0=seg7code[sdata]|0x80; //输出个位 Delay(8); P2=P2|0xf0; P2=P2&0xf7; P0=seg7code[xiaoshu1]; //输出 Delay(8); P2=P2|0xf0; P2=P2&0xfb; P0=seg7code[xiaoshu2]; //输出小 Delay(4); P2=P2|0xf0; Delay(8); P2=P2|0xf0;P2=P2&0xdf; P0=seg7code[j/10]; //输出十位 Delay(8); P2=P2|0xf0; P2=P2&0xef; P0=seg7code[j]|0x80; //输出个位 Delay(4); P2=P2|0xf0; */ if(sdata<=j) { lan=0; hong=1; sheng=1; }; if(sdata>=j) { lan=1; hong=0; sheng=0; }; } if(fg==0) //温度为负时显示的据 { P2=P2&0xef; P0=seg7code[11]; //负号 Delay(8); P2=P2|0xf0; P2=P2&0xdf; P0=seg7code[sdata/10]|0x80; //输出十位 Delay(8); P2=P2|0xf0; P2=P2&0xbf; P0=seg7code[sdata]; //输出个位 Delay(8); P2=P2|0xf0; P2=P2&0x7f; P0=seg7code[xiaoshu1]; //输出小 Delay(4); P2=P2|0xf0; } } main() {fg=1; x24c08_init(); //初始化24C08 j=x24c08_read(2);//读出保存的据 while(1) { DS18B20PRO(); Led(); if(jia==0) {Delay(200);j++;} if(jian==0) {Delay(200);j--;} x24c08_write(2,j); } }

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧