---
tags:
    - "String"
    - "Convert"
    - "Cast"
    - "To Number"
    - "Float"
    - "Integer"
    - "Decimal"
---

# Parse Number

[Strings](index.md) / **Parse Number**

<div class="grid cards" markdown>

* :green_circle: Can Read

* :red_circle: Cannot Write

</div>

## Description

Reads a text as a number. Returns the fallback value when the text cannot be read

## Parameters

| Name | Description |
| :--- | :---------- |
| Text | The text that is interpreted as a number |
| Fallback | The value returned when the text is not a valid number |

## Code

```cs
using System;
using System.Globalization;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Get]

    [Title("Parse Number")]
    [Category("Strings/Parse Number")]

    [Description("Reads a text as a number. Returns the fallback value when the text cannot be read")]
    [Image(typeof(IconNumber), ColorTheme.Type.Yellow, typeof(BadgeString))]

    [Parameter("Text", "The text that is interpreted as a number")]
    [Parameter("Fallback", "The value returned when the text is not a valid number")]

    [Keywords("String", "Convert", "Cast", "To Number", "Float", "Integer", "Decimal")]

    [Serializable]
    public class TypeNumberStringParse : TypeNumber
    {
        [SerializeField] private GetString m_Text = new GetString();
        [SerializeField] private GetNumber m_Fallback = new GetNumber(0f);

        public override double GetPreview(GameObject source) => Parse(
            this.m_Text.GetPreview(source),
            this.m_Fallback.GetPreview(source)
        );

        public override double Get(Args args) => Parse(
            this.m_Text.Get(args),
            this.m_Fallback.Get(args)
        );

        private static double Parse(string text, double fallback)
        {
            if (string.IsNullOrEmpty(text)) return fallback;

            return double.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out double value)
                ? value
                : fallback;
        }

        public override string ToString()
        {
            return $"Number of {this.m_Text}";
        }

        protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
    }
}

```

