Gottumukkala Sravan Kumar
Updated date Aug 27, 2022
In this blog, we will see how to convert the given string into Decimal in Python. There are several ways to convert the given input string into decimal.

In this blog, we will see how to convert the given string into Decimal in Python. There are several ways to convert the given input string into decimal. Let's see them one by one.

Before going to discuss, If we want to return the type of the variable in python, then we can use the `type()` method.

### Syntax:

``type(variable)``

Where the variable holds the value.

We can use the below methods to convert a String to Decimal,

• Convert a String to Decimal using `float()`
• Convert a String to Decimal using `numpy.float_()`
• Convert a String to Decimal using `decimal.Decimal()`

## Method 1: Convert a String to Decimal using float()

`float()` in Python converts the string with numeric data into decimals. If we display the type of the converted string, then class-float is returned.

No built-in module is required to use this method.

### Syntax:

``float(string)``

### Parameter:

It takes only one parameter. IE string.

### Example:

Convert the string - ‘58.99’ into decimal using the `float()` method.

``````# Input string
string_1='58.99'

print("Actual string: ", string_1)
print("Actual string type: ", type(string_1))

# Convert into float
print("Final value: ", float(string_1))
print("Final string type: ", type(float(string_1)))``````

### Output:

``````Actual string:  58.99
Final value:  58.99
Actual string type:  <class 'str'>
Final string type:  <class 'float'>``````

We can see that before the variable type is `str (string)`, it is float.

## Method 2: Convert a String to Decimal using numpy.float_()

`float_() `in Python is available in the numpy module that converts the string that has numeric data into decimal. It is similar to the first method.

This method is useful if we store the string values in numpy arrays.

### Syntax:

``numpy.float_(string/numpy_array)``

### Parameter:

It takes only one parameter. IE string or numpy_array that have strings.

### Example 1:

Convert the string - ‘58.99’ into decimal using `numpy.float_()` method.

``````# Import the numpy module
import numpy

# Input string
string_1='58.99'
print("Actual string: ", string_1)

print("Final value: ", numpy.float_(string_1))
print("Final string type: ", type(numpy.float_(string_1)))``````

### Output:

``````Actual string:  58.99
Final value:  58.99
Final string type:  <class 'numpy.float64'>``````

We can see that before the type of variable type is `str (string)`, float.

### Example 2:

Now convert the strings present in the numpy array into decimal using `numpy.float_()` method.

``````# Import the numpy module
import numpy

# Input strings
array=numpy.array(['58.99','45.67','78.89'])
print("Actual strings: ", array)

# Convert into float
print("Final value: ", numpy.float_(array))``````

### Output:

``````Actual strings:  ['58.99' '45.67' '78.89']
Final value:  [58.99 45.67 78.89]``````

## Method 3: Convert a String to Decimal using Decimal()

`Decimal()` in Python is available in the decimal module that converts the string or float value into decimal and returns the type as a `decimal.Decimal()`.

It is important to import this module, Otherwise, you will get a module not found error.

### Syntax:

``decimal.Decimal(string)``

### Parameter:

It takes only one parameter. IE string.

### Example:

Convert the string - ‘58.99’ into decimal using `decimal.Decimal()` method.

``````# Import the Decimal method from decimal module
from decimal import Decimal

# Input string
string_1='58.99'

print("Actual string: ",string_1)
print("Actual string type: ",type(string_1))

print("Final value: ",Decimal(string_1))
print("Final string type: ",type(Decimal(string_1)))``````

### Output:

``````Actual string:  58.99
Actual string type:  <class 'str'>
Final value:  58.99
Final string type:  <class 'decimal.Decimal'>``````

We can see that before the type of the variable is str (string), and After that it is decimal.

## Summary

So by the end of this blog, we have seen how to convert the string into decimal using three methods.