Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7409189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:02:43+00:00 2026-05-29T06:02:43+00:00

IF gives the wrong answer when I try to compare 2 large numbers. For

  • 0

IF gives the wrong answer when I try to compare 2 large numbers.

For example, this simple batch file

@echo off
setlocal
set n1=30000000000000
set n2=40000000000
if %n1% gtr %n2% echo %n1% is greater than %n2%
if %n1% lss %n2% echo %n1% is less than %n2%
if %n1% equ %n2% echo %n1% is equal to %n2%

produces

30000000000000 is equal to 40000000000

What is going on, and how do I fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:02:43+00:00Added an answer on May 29, 2026 at 6:02 am

    If both sides of an IF comparison are composed strictly of decimal digits, then IF will interpret both sides as numbers. This is what enables IF to correctly determine that 10 is greater than 9. If you have any non digit characters, then IF does a string comparison. For example, “10” is less than “9” because the quotes are not digits, and 1 sorts lower than 9.

    The reason the comparison in the question fails is because CMD.EXE cannot process numbers larger than 2147483647. An odd design quirk in IF treats any number larger than 2147483647 as being equal to 2147483647.

    If you want to do a string comparison of large numbers, then the solution is easy. You just need to add 1 or more non digit characters to both sides of the condition. The following script –

    @echo off
    setlocal
    set n1=30000000000000
    set n2=40000000000
    if "%n1%" gtr "%n2%" echo "%n1%" is greater than "%n2%"
    if "%n1%" lss "%n2%" echo "%n1%" is less than "%n2%"
    if "%n1%" equ "%n2%" echo "%n1%" is equal to "%n2%"
    

    produces the correct string comparison result

    "30000000000000" is less than "40000000000"
    

    But in most cases, this is not what is wanted.

    If you want to do a numeric comparison, then the process is a bit more involved. You need to convert the number into a string that will sort properly as a number. This is accomplished by prefixing the numeric string with zeros in a way that makes both numeric strings the same width. The simplest solution is to determine the maximum number of digits you need to support – let’s say 15 for this example. So you prefix each value with 15 zeros, and then preserve only the right-most 15 characters by using a substring operation. You also need to add a non-digit to both sides as before – again quotes work well.

    This script –

    @echo off
    setlocal
    set n1=30000000000000
    set n2=40000000000
    call :padNum n1
    call :padNum n2
    if "%n1%" gtr "%n2%" echo %n1% is greater than %n2%
    if "%n1%" lss "%n2%" echo %n1% is less than %n2%
    if "%n1%" equ "%n2%" echo %n1% is equal to %n2%
    exit /b
    
    :padNum
    setlocal enableDelayedExpansion
    set "n=000000000000000!%~1!"
    set "n=!n:~-15!"
    endlocal & set "%~1=%n%"
    exit /b
    

    produces –

    030000000000000 is greater than 000040000000000
    

    Note that left prefixing with spaces works just as well as zeros.

    You can later remove the leading zeros whenever you want using the following (or adapt to remove leading spaces)

    for /f "tokens=* delims=0" %%A in ("%n1%") do set "n1=%%A"
    if not defined n1 set "n1=0"
    

    Normally we don’t deal with large numbers in batch files. But they can easily crop up if we look at free space on a hard disk. Terabyte disk drives are now relatively inexpensive. This is how I first ran into comparison of large numbers at https://stackoverflow.com/a/9099542/1012053

    I chose to support 15 digits in my example because that equates to almost 999 terabytes. I imagine it will be a while before we have to deal with disk drives larger than that. (But who knows!)

    EDIT – My description of how IF parses numbers is intentionally overly simplistic. IF actually supports negative numbers, as well as hex and octal notation. See Rules for how CMD.EXE parses numbers for a much more thorough explanation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ANSWER: There isn't a natively managed equivalent for this method. However, a good example
I don't see anything that I am doing wrong, but NetBeans gives me the
This code in JS gives me a popup saying i think null is a
I've scavenged on every single topic on this forum to try and find an
I'm sure there is an answer to this somewhere but I'm clearly using the
I'm trying to parse a simple JSON file. I'm very new to javascript, JSON,
I'm trying to parse a simple JSON file. I'm very new to javascript, JSON,
This question follows on from the answer given by Michael Pilat in Preventing Plus
Ok, I think this question is at the wrong place and I'll head over
When i give wrong number of parameters in a function , i get errors.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.