批舍入一个数字 [英] Batch rounding a number

查看:126
本文介绍了批舍入一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计算字节的磁盘空间的脚本。我已经找到一种方法将其转换为兆字节。我刚才运行它,我得到了这一点: 1867.603187561035 。有没有四舍五入的方式是它会更加清晰,如果说 1868 。感谢您的帮助。


解决方案

 关闭@echo
设置number_to_round = 1867.603187561035FOR / F令牌= 1,2 = delims。 %%一个在(%number_to_round%)做(
  设置first_part = %%一
  设置SECOND_PART = %% b
)设置SECOND_PART =%SECOND_PART:〜0.1%
回声%SECOND_PART%
如果定义SECOND_PART如果%SECOND_PART%GEQ 5(    集/圆角=%first_part%+ 1
)其他(
    集/一个圆形=%first_part%
)回声%圆润%

(用JavaScript调用,您可以得到更多的precise结果,并与长一些工作(当批是指不限整数))

 关闭@echo
设置number_to_round = 1867.603187561035设置beginJS = MSHTA的javascript:(新ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(Math.round(%number_to_round%)\"
设置endJS =));,FOR / F %% N的(
  %beginJS %% endJS%
)确实设置四舍五入= %%ñ回声%圆润%

I have a script that calculates disk space in bytes. I have found a way to convert it to megabytes. I have just run it and i got a this: 1867.603187561035. Is there a way of rounding it is it would be much clearer if it said 1868. Thanks for any help.

解决方案

@echo off
set "number_to_round=1867.603187561035"

for /f "tokens=1,2 delims=." %%a  in ("%number_to_round%") do (
  set first_part=%%a
  set second_part=%%b
)

set second_part=%second_part:~0,1%
echo %second_part%
if defined second_part if %second_part% GEQ 5 ( 

    set /a rounded=%first_part%+1
) else ( 
    set /a rounded=%first_part%
)

echo %rounded%

OR (with the javascript call you can get more precise results and work with long number (while batch is limited to integers))

@echo off
set number_to_round=1867.603187561035

set "beginJS=mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(Math.round(%number_to_round%)"
set "endJS=));""

for /f %%N in (
  '%beginJS%%endJS%'
) do set rounded=%%N

echo %rounded%

这篇关于批舍入一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆