Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 699
  • Last Modified:

How to truncate a number in a batch file

I have a number with a 3 digits after the decimal point.  For example, I want to be able to truncate 10000.000 to just display 10000.  For some reason, I can only seem to do this with numbers smaller than 10000.  I have been doing it this way:

set /n = 10000.999
set a/ n = %n:~0,-4%

If the number is 5 digits or more with 3 decimal places(xxxxx.xxx), it will not work.  In other words, if the number is over 10000 and has 3 decimal digits, it ignores the decimal and truncates the 10000 4 digits to the number 1.

I am actually doing this in a for loop:
for /f "tokens=1,2,3,8 delims=\ " %%a in (myfile.txt) do (...

and in this loop I tried to truncate the %%a variable.  I tried set /a %%a = %%a:~0,-4 but that did not work.  What I am trying to do now is this
set /a n=%%a
set /a %n:~0,-4%

This works until I get to a number that is higher than 10000.000
I would appreciate any help.
Thanks!
0
searching_5000
Asked:
searching_5000
  • 3
  • 2
1 Solution
 
searching_5000Author Commented:
Oops,
In my example, I meant to type:

set n = 10000.999
set a/ n = %n:~0,-4%

Not "/n"
0
 
oBdACommented:
Try
for /f "tokens=1 delims=." %%a in ("%n%") do set n=%%a
0
 
searching_5000Author Commented:
I need to have / as a delimiter...
for /f "tokens=1,2,3,8 delims=\ " ...
Can I have more than one delims in a for loop?
0
 
oBdACommented:
Not instead of your for /f loop, but to truncate.
In the for loop, you could use something like this:

setlocal enabledelayedexpansion
for /f "tokens=1,2,3,8 delims=\ " %%a in (myfile.txt) do (
  for /f "tokens=1 delims=." %%i in ("%%a") do set n=%%i
  echo !n!
)
0
 
searching_5000Author Commented:
Perfect... it works great!  Thanks oBdA
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now