<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Casting a String (containing number in EU format) to a Decimal in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/casting-a-string-containing-number-in-eu-format-to-a-decimal/m-p/84194#M37156</link>
    <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I have a string column containing a number in EU format, has comma instead of dot, e.g. 10,35&lt;/P&gt;&lt;P&gt;I need to convert this string into a proper decimal data type as part data transformation into the target table.&lt;/P&gt;&lt;P&gt;I could do it as below by replacing the "," in the string to "." and then using the cast function, col_name is string column with values like 3,35&amp;nbsp; 10,4&amp;nbsp; &amp;nbsp;5,86&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;cast(replace(&lt;/SPAN&gt;&lt;SPAN&gt;col_name,&lt;/SPAN&gt;&lt;SPAN&gt;','&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;'.'&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN&gt;as&lt;/SPAN&gt; &lt;SPAN&gt;decimal(&lt;/SPAN&gt;&lt;SPAN&gt;10&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;4&lt;/SPAN&gt;&lt;SPAN&gt;))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;But, is there any direct function which handles "," to "." (format) conversion internally and delivers the expected result?&lt;BR /&gt;&lt;BR /&gt;Many thanks!!&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Mon, 26 Aug 2024 09:24:52 GMT</pubDate>
    <dc:creator>Harsha777</dc:creator>
    <dc:date>2024-08-26T09:24:52Z</dc:date>
    <item>
      <title>Casting a String (containing number in EU format) to a Decimal</title>
      <link>https://community.databricks.com/t5/data-engineering/casting-a-string-containing-number-in-eu-format-to-a-decimal/m-p/84194#M37156</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I have a string column containing a number in EU format, has comma instead of dot, e.g. 10,35&lt;/P&gt;&lt;P&gt;I need to convert this string into a proper decimal data type as part data transformation into the target table.&lt;/P&gt;&lt;P&gt;I could do it as below by replacing the "," in the string to "." and then using the cast function, col_name is string column with values like 3,35&amp;nbsp; 10,4&amp;nbsp; &amp;nbsp;5,86&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;cast(replace(&lt;/SPAN&gt;&lt;SPAN&gt;col_name,&lt;/SPAN&gt;&lt;SPAN&gt;','&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;'.'&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN&gt;as&lt;/SPAN&gt; &lt;SPAN&gt;decimal(&lt;/SPAN&gt;&lt;SPAN&gt;10&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;4&lt;/SPAN&gt;&lt;SPAN&gt;))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;But, is there any direct function which handles "," to "." (format) conversion internally and delivers the expected result?&lt;BR /&gt;&lt;BR /&gt;Many thanks!!&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 26 Aug 2024 09:24:52 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/casting-a-string-containing-number-in-eu-format-to-a-decimal/m-p/84194#M37156</guid>
      <dc:creator>Harsha777</dc:creator>
      <dc:date>2024-08-26T09:24:52Z</dc:date>
    </item>
    <item>
      <title>Re: Casting a String (containing number in EU format) to a Decimal</title>
      <link>https://community.databricks.com/t5/data-engineering/casting-a-string-containing-number-in-eu-format-to-a-decimal/m-p/84214#M37162</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/111456"&gt;@Harsha777&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Your solution looks good!&lt;/P&gt;&lt;P&gt;However, you may try also&amp;nbsp;&lt;A href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.to_number.html" target="_self"&gt;to_number&lt;/A&gt; function, but unfortunately still will need to first to replace "," with ".".&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;LI-CODE lang="python"&gt;from pyspark.sql.functions import to_number, regexp_replace, lit

data = [("10,6523",), ("10,23",)]
df = spark.createDataFrame(data, ["col_name"])

df = df.withColumn("decimal_col", to_number(regexp_replace("col_name", ",", "."), lit("9999999.9999")))

df.show()&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="filipniziol_0-1724674833316.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/10592iA71D5D7C8F00DA17/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="filipniziol_0-1724674833316.png" alt="filipniziol_0-1724674833316.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Without replacement it will just remove the comma:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark.sql.functions import to_number, lit

data = [("10,6523",), ("10,23",)]
df = spark.createDataFrame(data, ["col_name"])

df = df.withColumn("decimal_col", to_number("col_name", lit("99,9999")))

df.show()&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="filipniziol_1-1724674921590.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/10593i4A431936F0CA081C/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="filipniziol_1-1724674921590.png" alt="filipniziol_1-1724674921590.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Aug 2024 12:28:18 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/casting-a-string-containing-number-in-eu-format-to-a-decimal/m-p/84214#M37162</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-08-26T12:28:18Z</dc:date>
    </item>
  </channel>
</rss>

