C++ std::stold() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::stold

Definition

In C++, std::stold() converts a string to long double.

Include <string>.

This is a free function from <string> that works with std::string (and sometimes wstring). Member functions like find live on the string object with a dot; helpers like this are called as std::stold(...).

Syntax

Pass the text.

C++
long double x = std::stold("1.23456789012345");
CallWhat it does
std::stold(str)Parse as long double.

Precision of long double depends on the platform.

Library declarations
C++
long double stold(const string& str, size_t* idx = 0);

Parameters

ParameterDescription
strInput.
idxOptional.

Return Value

Return ValueDescription
long doubleParsed value.

C++ Examples

Example 1 — Basic stold()

Convert to long double.

C++
#include <iostream>
#include <string>

int main() {
    long double x = std::stold("1.5");
    std::cout << (double)x << '\\n';
    return 0;
}

How It Works

  1. Text parses as long double.
  2. We cast to double for simple printing.
  3. Value is 1.5.

Example 2 — stold Scientific

Parse scientific notation.

C++
#include <iostream>
#include <string>

int main() {
    long double x = std::stold("2e2");
    std::cout << (double)x << '\\n';
    return 0;
}

How It Works

  1. 2e2 is 200.
  2. stold accepts the exponent form.
  3. Printed as 200.

Example 3 — stold idx

Capture end index.

C++
#include <iostream>
#include <string>

int main() {
    std::size_t i = 0;
    long double x = std::stold("8.0!", &i);
    std::cout << (double)x << ' ' << i << '\\n';
    return 0;
}

How It Works

  1. Parses 8.0.
  2. Stops at '!'.
  3. idx is 3.

Frequently Asked Questions

String to long double.
When you want the widest floating type on the platform.
Not guaranteed on every platform.
<string>.
Yes on invalid/out of range.

Next: stoll()

Keep going through the string functions. Convert a string to long long.

stoll() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful