How to resolve the algorithm Determine if a string is numeric step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Determine if a string is numeric step by step in the Nim programming language

Table of Contents

Problem Statement

Create a boolean function which takes in a string and tells whether it is a numeric string (floating point and negative numbers included) in the syntax the language uses for numeric literals or numbers converted from strings.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Determine if a string is numeric step by step in the Nim programming language

Source code in the nim programming language

import strutils

proc isNumeric(s: string): bool =
  try:
    discard s.parseFloat()
    result = true
  except ValueError:
    result = false

const Strings = ["1", "3.14", "-100", "1e2", "Inf", "rose"]

for s in Strings:
  echo s, " is ", if s.isNumeric(): "" else: "not ", "numeric"


import parseutils

proc isNumeric(s: string): bool =
  var x: float
  s.parseFloat(x) == s.len

const Strings = ["1", "3.14", "-100", "1e2", "Inf", "rose"]

for s in Strings:
  echo s, " is ", if s.isNumeric(): "" else: "not ", "numeric"


  

You may also check:How to resolve the algorithm Higher-order functions step by step in the Inform 6 programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the Z80 Assembly programming language
You may also check:How to resolve the algorithm Pragmatic directives step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Summarize and say sequence step by step in the Raku programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the Ruby programming language