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

# 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
• 3
• 2
1 Solution

Author Commented:
Oops,
In my example, I meant to type:

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

Not "/n"
0

Commented:
Try
for /f "tokens=1 delims=." %%a in ("%n%") do set n=%%a
0

Author 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

Commented:
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

Author Commented:
Perfect... it works great!  Thanks oBdA
0

## Featured Post

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