# Faster string to double

**URL:** <https://fortran-lang.discourse.group/t/faster-string-to-double/2208>\
**Category:** Uncategorized\
**Created:** [October 31, 2021, 5:35pm UTC](https://fortran-lang.discourse.group/t/faster-string-to-double/2208 "2021-10-31T17:35:21Z")\
**Posts on this page:** 1\
**Showing post:** 71

<div class="post-metadata">

**Author:** ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)\
**Post date:** [January 18, 2023, 8:14am UTC](https://fortran-lang.discourse.group/t/faster-string-to-double/2208/71 "2023-01-18T08:14:10Z")

</div>

You can check out the latest implementation [str2num\_m](https://github.com/jalvesz/Fortran-String-to-Num/blob/main/src/str2num_m.f90)

The core algorithm is now in an elemental subroutine:

```auto
elemental subroutine str2real_base(s,r,p,stat)
    ! -- In/out Variables
    character(*), intent(in) :: s !< input string
    real(wp), intent(inout) :: r !< Output real value
    integer(1), intent(out) :: p !< position within the number
    integer(1), intent(out) :: stat !< status upon success or failure to read
...
end subroutine

```

And both user interfaces are kept with the functions

```auto
elemental function str2real(s) result(r)
    ...
    call str2real_base(s,r,p,stat)
end function

function str2real_p(s,stat) result(r)
    character(len=:), pointer :: s !< input string
    ...
    call str2real_base(s,r,p,err)
    p = min( p , len(s) )
    s => s(p:)
    if(present(stat)) stat = err
end function

```

I did nothing fancy regarding the error status for the moment. just a constant value at the beginning and 0 if success

---

_[View the full topic](https://fortran-lang.discourse.group/t/faster-string-to-double/2208)._
