Home > Software design >  Is there a Regex to trim numbers after two decimal places?
Is there a Regex to trim numbers after two decimal places?

Time:09-24

I am trying to reduce the number of decimals and for some reason, using a "fixed decimal" 258.2 does not change all numbers in my column to two decimal places.

Pretty much I have the following after specifying the number as a fixed decimal with 2 places:

  1. 6.933141
  2. 5.13
  3. 1.56
  4. 2.94
  5. 1.54
  6. 6.470931

So changing the amount of fixed decimals did not do it for me, so I have been trying to use RegEx, and came up with (^\d .\d{2}). This however only identifies what I want to keep.

Is there a way to do this using Regex_Replace?

Thank you all in advance for your help!

CodePudding user response:

Use

^(\d \.\d{2})\d $

Replacement: $1. See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to $1:
--------------------------------------------------------------------------------
    \d                       digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \d{2}                    digits (0-9) (2 times)
--------------------------------------------------------------------------------
  )                        end of $1
--------------------------------------------------------------------------------
  \d                       digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
  • Related