Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
  • Language Reference Guide
    • Readme
    • Types in the Nu Language
      • Basic Types
        • Any
        • Number
        • Boolean
        • Integer
        • Float
        • Filesize
        • Duration
        • Datetime
        • Range
        • String
        • Record
        • List
        • Table
        • Closure
        • Nothing
        • Binary
        • Glob
        • Cell-Path
      • Other Data Types

        • Types used only in command signatures
          • Path
          • External argument
        • Types which are not declarable
          • Error
          • CustomValue
          • Block
      • Type signatures
      • Commands that interact with types
    • Operators
    • Flow control
      • if/else
      • loop
      • for
      • while
      • match
      • try/catch
      • break
      • return
      • continue
    • Filters
      • each and par-each
      • Filters to select subsets of data
      • where and filter
      • Understanding the difference between get and select
      • Adding and changing data
      • Summarizing and comparing lists
      • record commands
    • Matrices
    • Custom Commands
    • Declarations
    • Variable Scope
    • Strings and Text Formatting
    • Helpers and debugging commands
    • Pipelines
    • MIME Types for Nushell

Number

Description:When used in a type annotation or signature, matches either an int or a float.
Annotation:number
Literal syntax:N/A - Any int or float literal can be assigned to a number
Casts:N/A

Additional Language Notes

  1. number is a type for annotations, like any. No single value has the type number. A value annotated as number keeps its own type, so describe reports int or float:

    def kind [x: number] { $x | describe }
    [(kind 1) (kind 1.5)] | to nuon
    # => [int, float]

    describe shows number only for a collection that mixes ints and floats:

    [1 2.5] | describe
    # => list<number>
  2. Other types are rejected, including text that looks like a number and the number-like filesize and duration types. A literal is checked when the code is parsed:

    let n: number = "1"
    # => Error: nu::parser::type_mismatch
    # =>
    # =>   × Type mismatch.
    # =>    ╭─[repl_entry #1:1:17]
    # =>  1 │ let n: number = "1"
    # =>    ·                 ─┬─
    # =>    ·                  ╰── expected number, found string
    # =>    ╰────

    Convert text with into int or into float first.

  3. A mut variable without an annotation keeps the type of its first value, so an int variable can't later hold a float. Annotate it as number to allow both:

    mut total: number = 0
    $total = $total + 2.5
    $total
    # => 2.5

    Without the annotation, $total = 2.5 after mut total = 0 fails with nu::parser::operator_incompatible_types.

  4. number also works as an input or output type, and inside other types such as list<number>:

    def half []: number -> float { $in / 2 }
    [(4 | half) (1.5 | half)] | to nuon
    # => [2.0, 0.75]

    Many built-in commands use it this way. help math abs, for example, lists number and list<number> among its input types.

  5. A number can be passed where an int or a float is expected. The parser lets it through, and the value is checked when the command runs. See Which values a type accepts.

Edit this page on GitHub
Contributors: fdncred
Prev
Any
Next
Boolean