批量生成3位长度的小数与2位长度小数乘法题目
方便广大数学老师或学生家长打印练习。
SQL SERVER 代码
–1.生成随机数
 select top 5000
 ceiling(rand(checksum(NEWID())) *1000) as x –生成3位以内随机整数
 ,ceiling(rand(checksum(NEWID())) *100) as y  –生成2位以内随机整数
 –cast(cast(rand(rand(number)100000000)10000000000000 as bigint)%1000 as dec(10,4)) as x    –生成3位以内随机整数
 –,cast(cast(rand(rand(number)100000000)1000000000000000 as bigint)%100 as dec(10,4)) as y   –生成2位以内随机整数
 –,number,rand(number)100000000,rand(rand(number)100000000)
 ,cast(0 as dec(28,6))as z
 into #temp01
 from master..spt_values a,master..spt_values b  
 where 1=1 
–将整数转换为随机位小数
 update a set 
 x=x/(case when cast(rand(x)100000000 as int)%4=1 then 1000 when cast(rand(x)100000000 as int)%4=2 then 100 when cast(rand(x)100000000 as int)%4=3 then 10 else 1 end), –将3位整数随机转换为小数 y=y/(case when cast(rand(y)100000000 as int)%3=1 then 100 when cast(rand(y)*100000000 as int)%3=2 then 10 else 1 end )–将2位整数随机转换为小数
 from #temp01 a
–2.清理数据
 update #temp01 set z=x*y    –计算乘积得数
–去除得数为0的计算
 delete from #temp01 where z=0
–去除全整数计算
 delete from #temp01 where (x-cast(x as int)=0 and y-cast(y as int)=0)
–去除两个乘数都是小数的计算
delete from #temp01 where cast(x as int)=0 and cast(y as int)=0
–去除有一个乘数是1的计算
delete from #temp01 where x=1 or y=1
–3.输出结果
 –select * from #temp01
 –输出小数乘法题
 select cast(x as float)as 乘数,’x’ 乘,cast(y as float)as 乘数,’=’as 等于,cast(z as float)as 积 from #temp01 order by NEWID()
 –输出小数除法题
 select cast(z as float)as  被除数,’÷’ 除,cast(y as float)as 除数,’=’as 等于,cast(x as float)as 商 from #temp01 order by NEWID()
输出结果:
乘数    乘   乘数  等于  积
 27.9    x   0.8 =   22.32
 33.6    x   9.2 =   309.12
 0.18    x   14  =   2.52
 0.425    x   4.2 =   1.785
 被除数    除   除数 等于  商
 3312.4    ÷   5.2 =   637
 72.416    ÷   7.3 =   9.92
 7.5287    ÷   0.79    =   9.53
 37.037    ÷   77  =   0.481
代码下载: 小数计算题随机生成.sql
5000道题目下载: 小数计算题一万道
© 2020, ITJOY.NET. 版权所有. 如未注明,均为原创,转载请注明出处。
