Board logo

标题: 如何用cmd做四舍五入运算? [打印本页]

作者: peterdocter     时间: 2007-4-1 12:42    标题: 如何用cmd做四舍五入运算?

可以做到吗?
作者: bjsh     时间: 2007-4-1 22:55
可以
作者: bjsh     时间: 2007-4-1 22:55
把你的要求再具体一下;

比如举个例子上来;试试帮你写写代码
作者: bjsh     时间: 2007-4-2 02:40
试试这个吧

  Quote:

  1. @echo off &SetLocal EnableDelayedExpansion
  2. set /p x=请输入被除数:
  3. set /p y=请输入除数:
  4. set /p n=请输入欲保留的小数的位数:
  5. set /a count=0
  6. set /a bjsh=10
  7. :loop
  8. if %count% geq %n% goto start
  9. set /a bjsh*=10
  10. set /a count+=1
  11. goto loop
  12. :start
  13. set /a rest=%x%*%bjsh%/%y%-%x%*%bjsh%/10/%y%*10
  14. if "%n%"=="0" (set /a last=%x%/%y%) else (
  15. set /a last=%x%*%bjsh%/10/%y%-%x%*%bjsh%/100/%y%*10
  16. )
  17. if %rest% geq 5 set /a last+=1
  18. if "%n%"=="0" echo 结果为%last% & pause & goto exit
  19. set /a result=%x%/%y%
  20. set result=%result%.
  21. set /a end=%n%-1
  22. set /a bjsh=10
  23. for /l %%i in (1,1,%end%) do (
  24. set /a add=%x%*!bjsh!/%y%-%x%*!bjsh!/10/%y%*10
  25. set result=!result!!add!
  26. set /a bjsh*=10
  27. )
  28. set result=%result%%last%
  29. echo 结果为%result% & pause
  30. :exit
         BJSH发表于:  2007-04-01  13:30


作者: flyinspace     时间: 2007-4-2 02:46
哎。bjsh兄

楼主只是需要四舍五入。
你写那么多。有用么?
作者: bjsh     时间: 2007-4-2 03:01
呵呵
那倒是;
我只是顺手从发在你楼里的代码复制过来了