Text to Number in Grasshopper: Solving Conversion Errors

Text to Number in Grasshopper - Tutorial
Contents

Few situations in Grasshopper are more annoying than a component turning red and throwing an error for seemingly no reason. This is often the case when you’re trying to convert text to a number in Grasshopper. In this post we’ll go through why it happens, and ways to fix it.

Let’s dive in!

When a Number Isn’t a Number: Example Problem

Let’s consider an example. In the script below I’m trying to calculate the average of a list of numbers, but the Average component turns red and doesn’t return an output.

By right-clicking on the component and selecting ‘Runtime Errors‘ we can see the full error message: “Solution exception: GH_String cannot be cast to System.Double“.

But they are obviously numbers, so what’s going on?

Why Text Isn’t Recognized as a Number

The reason the error shows up, is that in Grasshopper a number can be one of the following two things:

  • A digit as part of a text string, for example “There are 32 flamingoes”
  • An actual numerical value: 3.46574

Which one it is largely depends on where the number came from.

This is because depending on the source, Grasshopper will assign a certain datatype, like text (string) or number to a component’s output. And while many components have a built-in datatype conversion feature that will automatically convert one datatype to another (if possible), some components don’t.

The Panel component is a great example for that: it is primarily meant to contain and display text. Therefore Grasshopper assumes its output is text. And since the Average (Avr) component doesn’t come with a datatype auto-conversion feature, we get the error.

Luckily, this error is easy to solve!

How to Convert Text into a Number in Grasshopper

To convert a number that Grasshopper considers ‘text’ to an actual number, we can use the Number (Num) container’s auto-conversion feature.

The Number (Num) component is one of the many data container components available in Grasshopper. It’s located in the Params component tab, grouped under Primitive.

How to Add the Number container component in Grasshopper

Grasshopper’s container components automatically convert inputs to the target datatype they represent – where possible.

In our case, we’ll insert a Number (Num) component between the numbers in the Panel and the Average (Avr) component.

How to Convert Text to number in Grasshopper

The Average (Avr) component now recognizes the numbers and returns the expected result!

‘Data conversion failed from text to number’ – Dealing with Non-Numerical Values

The Number (Num) component works magic in many cases, but it can only convert text into numbers if the input is a number to begin with!

Now how can you deal with odd values in a list that aren’t numerical?

Let’s say we loaded a list of values from an external file. The list contains mostly numbers but some of the fields contain an asterisk (*) to mark missing data. If we try to process this input as numbers, Grasshopper will return the error ‘Data conversion failed from text to number’.

Data conversion failed from text to number error

In most cases the components will turn red, but still return the numbers, with the exception of the failed elements in the list which will return ‘Null’.

The best way to deal with this error is to replace the non-numerical values.

We can do so with the Replace Text (Rep) component.

For this to work, we need to treat the numbers as text input, and then convert them to numbers again afterwards. Luckily all the converting is going on behind the scenes automatically.

The Replace Text (Rep) component replaces any occurrence of a subset of characters in a given text. You can find it in the ‘Sets’ component tab, in the ‘Text’ group.

How to Replace Text in Grasshopper

Replace Text (Rep) has three inputs:

  • the Text (T) to operate on
  • the Text Fragment (F) within the text to replace
  • the Text Replacement (R) text to replace it with

We can replace a single character, like in our case, but also entire text fragments, made up of several characters.

Following our example, we define the asterisk (*) as the fragment to replace in the Text Fragment (F) input, and a number (100) to replace it with in the Text Replacement (R) input.

How to fix the Data conversion failed from text to number error

As the Panel shows, all instances of (*) are replaced with our chosen replacement number of 100.

I hope you’ve found this useful!

Happy designing!

Like this article?
Share on LinkedIn
Picture of Thomas Tait
Thomas Tait
Architect at Snøhetta and Head Instructor @ Hopific (or, in plain English, I help designers use Grasshopper to supercharge their designs.
Ready to Dive into Grasshopper? Grab Your Free Ebook!

Accelerate Your Learning and Get Up and Running Quickly.

More Great Reads