# From string to float without change

**URL:** <https://discourse.mcneel.com/t/from-string-to-float-without-change/68697>\
**Category:** Grasshopper Developer\
**Created:** [August 13, 2018, 9:30am UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697 "2018-08-13T09:30:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nader\_Belal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nader_belal/32/500763_2.png) [@Nader\_Belal](https://discourse.mcneel.com/u/Nader_Belal)\
**Post date:** [August 13, 2018, 9:30am UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/1 "2018-08-13T09:30:06Z")

</div>

Hi there,

I was trying to write a python script to extract a list of values expressed as string, my problem is that when I use the float() function, it changes the values as we can see in the pic

 ![image](https://global.discourse-cdn.com/mcneel/original/3X/7/e/7eed3f1e39ed4c04072378bbd3c3d52cc886fceb.png)

I would like to know how I can change strings to float without any change (_including the number of decimals_) ?

Thank you in advance

**PS:** There was no need for rhino file since it’s empty  
[GIS - World File Parameters - Python 20180813.002\_003.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/7/9/793fe8bae4acac85ab44827b7084fbd5d5725688.gh) (11.0 KB)  
[terreno-norte-17\_1.zip](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/a/d/ada651a3558d781dec260fccfe5d717a95ce7a6f.zip) (249 Bytes)

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [August 13, 2018, 12:09pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/2 "2018-08-13T12:09:34Z")

</div>

`float()` _should_ work. Are you sure it’s not the GH panel that is truncating the decimals/doing scientific notation? Maybe try to `print()` the values inside the GHPython editor to see what they are?

Also, we would need the .jgw file to inspect further.

---

<div class="post-metadata">

**Author:** ![Nader\_Belal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nader_belal/32/500763_2.png) [@Nader\_Belal](https://discourse.mcneel.com/u/Nader_Belal)\
**Post date:** [August 13, 2018, 12:17pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/3 "2018-08-13T12:17:06Z")

</div>

@AndersDeleuran

Just uploaded the jpw file, and that is the result of printing

 ![image](https://global.discourse-cdn.com/mcneel/original/3X/1/1/119badecbc840f0397db97756f866c0b8fea68a1.png)

Here I observed tht the last two numbers were shorten, which is also undesirable ☹

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [August 13, 2018, 1:07pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/4 "2018-08-13T13:07:05Z")

</div>

I _think_ this might simply be [the inherent floating point arithmetic](https://docs.python.org/2/tutorial/floatingpoint.html) (and more specifically displaying those), where you could use `Decimal()` to _“see the exact value that’s stored in any particular Python float”_:

 ![2018-08-13%2015_03_11-Grasshopper%20Python%20Script%20Editor](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/2/c/2c2029089225f620cc5e88e9553ca0fd1e28ab64.png)

---

<div class="post-metadata">

**Author:** ![Nader\_Belal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nader_belal/32/500763_2.png) [@Nader\_Belal](https://discourse.mcneel.com/u/Nader_Belal)\
**Post date:** [August 13, 2018, 1:50pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/5 "2018-08-13T13:50:21Z")

</div>

@AndersDeleuran

Are you working on a MAC ?

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [August 13, 2018, 2:01pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/6 "2018-08-13T14:01:51Z")

</div>

On a PC, Windows 10, using Rhino SR14 64-bit (5.14.522.8390, 05/22/2017) and GH 0.9.0076.

---

<div class="post-metadata">

**Author:** ![Nader\_Belal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nader_belal/32/500763_2.png) [@Nader\_Belal](https://discourse.mcneel.com/u/Nader_Belal)\
**Post date:** [August 13, 2018, 2:02pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/7 "2018-08-13T14:02:06Z")

</div>

Sorry to tell you that this way didn’t work, decimal.Decimal can’t be found

But I have found the solution:

```auto
import rhinoscriptsyntax as rs
import System.Convert as convert

def worldParam(file):
    paramList =[]
    
    with open(file, 'r', 0) as worldParamFile:
        for line in worldParamFile:

            paramList.append(convert.ToDecimal(line))
            
        return paramList

a = worldParam(worldFile)

```

The trick was in calling a dotNet System.Convert and the result can be seen in the following pic

 ![image](https://global.discourse-cdn.com/mcneel/original/3X/0/a/0a61940e14b2e12170fbe7627ad8121c3b917240.png)

thank you for your help.

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [August 13, 2018, 2:04pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/8 "2018-08-13T14:04:12Z")

</div>

> [@Nader\_Belal](#):
>
> decimal.Decimal can’t be found

It should be there, intellisense just doesn’t pick it up. But yes, going through .NET is definitely also an option.

---

<div class="post-metadata">

**Author:** ![Nader\_Belal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nader_belal/32/500763_2.png) [@Nader\_Belal](https://discourse.mcneel.com/u/Nader_Belal)\
**Post date:** [August 13, 2018, 2:41pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/9 "2018-08-13T14:41:57Z")

</div>

I think there is something funny.

 ![image](https://global.discourse-cdn.com/mcneel/original/3X/e/5/e5110673d30458da3f5e6b565b926f7d26ee721b.png)

as you can see from the pic, the result of my code is interpreteded as **Boolean** , if they’re passe through a Panel they are seen as **String**.

So how can I make sure that “a” will only export a float ???

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [August 13, 2018, 3:27pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/10 "2018-08-13T15:27:58Z")

</div>

> [@Nader\_Belal](#):
>
> So how can I make sure that “a” will only export a float ???

> [@Nader\_Belal](#):
>
> a = worldParam(worldFile)

Maybe?  
z = worldParam(worldFile)  
a = float(z)

---

<div class="post-metadata">

**Author:** ![Nader\_Belal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nader_belal/32/500763_2.png) [@Nader\_Belal](https://discourse.mcneel.com/u/Nader_Belal)\
**Post date:** [August 13, 2018, 3:35pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/11 "2018-08-13T15:35:32Z")

</div>

Didn’t work, float expected a string not a list

---

<div class="post-metadata">

**Author:** ![wim](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/wim/32/747482_2.png) [@wim](https://discourse.mcneel.com/u/wim)\
**Post date:** [August 13, 2018, 3:38pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/12 "2018-08-13T15:38:10Z")

</div>

> [@Nader\_Belal](#):
>
> if they’re passe through a Panel they are seen as **String**.

A quick remark from the side-line: passing anything through a panel will always convert that to a string. So: never-ever do that (unless you really want that result). \o/

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [August 13, 2018, 3:38pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/13 "2018-08-13T15:38:19Z")

</div>

loop through the values and add(append) them as floats in another list

```
list = ["1","2","2","3","4"]
list2=[]
for i in range(0, len(list)):
    list2.append(float(list[i]))

```

Then a = list2

---

<div class="post-metadata">

**Author:** ![Nader\_Belal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nader_belal/32/500763_2.png) [@Nader\_Belal](https://discourse.mcneel.com/u/Nader_Belal)\
**Post date:** [August 13, 2018, 3:47pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/14 "2018-08-13T15:47:52Z")

</div>

@ivelin.peychev  
I tried a similar method as was described up in the thread, and float have resulted in rounding my figures in a way I don’t need.

@wim  
Thank you for the remark, then here is the other question:

Why the values of a are exported as Boolean from the python component ??

---

<div class="post-metadata">

**Author:** ![Nader\_Belal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nader_belal/32/500763_2.png) [@Nader\_Belal](https://discourse.mcneel.com/u/Nader_Belal)\
**Post date:** [August 13, 2018, 3:59pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/15 "2018-08-13T15:59:06Z")

</div>

@ivelin.peychev & @wim

Here is the last modification of the file

[GIS - World File Parameters - Python 20180813.002\_003.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/d/c/dc9e7811159dbc1f9f27f69656276ab63fedfc71.gh) (7.5 KB)  
[terreno-norte-17\_1.zip](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/a/d/ada651a3558d781dec260fccfe5d717a95ce7a6f.zip) (249 Bytes)

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [August 13, 2018, 4:00pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/16 "2018-08-13T16:00:46Z")

</div>

![image](https://global.discourse-cdn.com/mcneel/original/3X/5/4/54939d9b335ffcccec1a2cbb6d68f72ea523f7af.png)

Google+StackOverflow+ some trial&error experiments

However apparently there’s a .16f limitation coming from Rhino itself. You can’t go further than 16 digits behind the decimal point

---

<div class="post-metadata">

**Author:** ![Nader\_Belal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nader_belal/32/500763_2.png) [@Nader\_Belal](https://discourse.mcneel.com/u/Nader_Belal)\
**Post date:** [August 13, 2018, 4:03pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/17 "2018-08-13T16:03:05Z")

</div>

> **[World file](https://en.wikipedia.org/wiki/World_file)**
>
> A world file is a six line plain text sidecar file used by geographic information systems (GIS) to georeference raster map images. The file specification was introduced by Esri, and consists of six coefficients of an affine transformation that describes the location, scale and rotation of a raster on a map.
> The generic meaning of the six parameters in a world file (as defined by Esri) are:
> This description is however misleading in that the D and B parameters are not angular rotations, and that ...

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [August 13, 2018, 4:11pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/18 "2018-08-13T16:11:45Z")

</div>

> [@ivelin.peychev](#):
>
> However apparently there’s a .16f limitation coming from Rhino itself. You can’t go further than 16 digits behind the decimal point

Correction, there’s a 16 digits overall limitation.  
It is reasonable though, if an object is 1 000 000 000 units of length then .0001 is of no relevance and if an object is 0.0001 units of length the 0.000100000000001 is of no relevance, nor is 100000.00 relevant to the latter.

😃 Ironically this is coming from a naval architect who wishes to maintain constant 0.001 mm accuracy for ships.

---

<div class="post-metadata">

**Author:** ![Nader\_Belal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nader_belal/32/500763_2.png) [@Nader\_Belal](https://discourse.mcneel.com/u/Nader_Belal)\
**Post date:** [August 13, 2018, 4:21pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/19 "2018-08-13T16:21:47Z")

</div>

@ivelin.peychev

I don’t know how to express to you that my problem now is that the component exports Boolean and not a number, not an integer and not a float, even when passing through a panel I don´t see a collection of Trues and False, but numbers.

Do you have an explanation for that ?

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [August 13, 2018, 4:30pm UTC](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697/20 "2018-08-13T16:30:48Z")

</div>

I believe this is because you function returns the list and not the values of the list, but I’m not that deep in programming to help you further.

[Next page](https://discourse.mcneel.com/t/from-string-to-float-without-change/68697.md?page=2)
