How to truncate a number in a batch file

Posted on 2005-03-31
Medium Priority
Last Modified: 2008-02-26
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.
Question by:searching_5000
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Author Comment

ID: 13674789
In my example, I meant to type:

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

Not "/n"
LVL 85

Expert Comment

ID: 13674800
for /f "tokens=1 delims=." %%a in ("%n%") do set n=%%a

Author Comment

ID: 13674926
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?
LVL 85

Accepted Solution

oBdA earned 500 total points
ID: 13675059
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!

Author Comment

ID: 13678058
Perfect... it works great!  Thanks oBdA

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question