LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only

Adding a decimal to string values

  • 1.  Adding a decimal to string values

    Employee
    Posted 03-03-2010 13:32

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Iain Sanders

    Hi Guys,

    Please advise how I can add a decimal point to the following strings:

    100 so that it reads: 1.00 and

    100y100 so that it reads: 1.00y1.00 and

    100y100y100 so that it reads: 1.00y1.00y1.00

    Many thanks,

    Iain


  • 2.  RE: Adding a decimal to string values

    Employee
    Posted 03-03-2010 13:46

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    newdata = data.regexSubstitute("100","1.00")

    I may need more context to give a good answer.


  • 3.  RE: Adding a decimal to string values

    Employee
    Posted 03-03-2010 13:56

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Iain Sanders

    Thanks.

    Will this replace every occurrence of 100 with 1.00?

    Iain


  • 4.  RE: Adding a decimal to string values

    Employee
    Posted 03-03-2010 14:08

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    Yes. And also stonysmith just caught me on this. I over complicated it by using one of the regex functions. The replace() function would be much simpler.

    newdata = data.replace("100","1.00")


  • 5.  RE: Adding a decimal to string values

    Employee
    Posted 03-03-2010 14:14

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Iain Sanders

    The data I have is in the data field called "Fixed1"

    Here is a summary of the various records I have to start with (shown below)

    The data consists of numbers imported as strings, separated by � delimiters.

    Each value should have 2 decimal places, but when the data is imported, the decimal place is removed, so that e.g.

    1.00�1.00 becomes 100�100,
    1.00�0.60 becomes 100�60,
    84.00�2.50 becomes 8400�250 etc...

    Basically, I want to be able to place the decimal point back into all the data.

    Here is a sample of the data imported:

    Fixed1
    100�100
    100�100
    100�100
    100�60

    100�100
    100�100
    100�100
    100�60
    100�100
    100�100
    100�100
    100�60
    100�100�100
    100�100
    100�60
    100�100
    100�100
    100�100
    100�100
    100�100
    100�100

    100�100
    100
    100�100


  • 6.  RE: Adding a decimal to string values

    Employee
    Posted 03-03-2010 14:15

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Iain Sanders

    The data I have is in the data field called "Fixed1"

    Here is a summary of the various records I have to start with (shown below)

    The data consists of numbers imported as strings, separated by � delimiters.

    Each value should have 2 decimal places, but when the data is imported, the decimal place is removed, so that e.g.

    1.00�1.00 becomes 100�100,
    1.00�0.60 becomes 100�60,
    84.00�2.50 becomes 8400�250 etc...

    Basically, I want to be able to place the decimal point back into all the data.

    Here is a sample of the data imported:

    Fixed1
    100�100
    100�100
    100�100
    100�60

    100�100
    100�100
    100�100
    100�60
    100�100
    100�100
    100�100
    100�60
    100�100�100
    100�100
    100�60
    100�100
    100�100
    100�100
    100�100
    100�100
    100�100

    100�100
    100
    100�100


  • 7.  RE: Adding a decimal to string values

    Employee
    Posted 03-03-2010 14:24

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Iain Sanders

    Thank you for your response - one last thing, how do I make this replacement valid for any number, and not just "100"?

    Originally posted by: ejones
    					

    Yes. And also stonysmith just caught me on this. I over complicated it by using one of the regex functions. The replace() function would be much simpler.

    newdata = data.replace("100","1.00")


  • 8.  RE: Adding a decimal to string values

    Employee
    Posted 03-03-2010 14:38

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    There is probably a way to do this with regex. But it would be hard to understand and hard to update. Here is how I would do it.

    fixed1_Array = Fixed1.split("�")
    fixed1_Array_Length = fixed1_Array.len()
    iter = 0
    new_Fixed1 = ""
    while iter < fixed1_Array_Length {
    temp = fixed1_Array[iter].int()
    temp_value = format("%d.%02d%"
    ,temp.div(100)
    ,temp.mod(100)
    )
    new_Fixed1 = new_Fixed1.joinStrings(temp_value,"�")
    iter = iter + 1
    }
    emit new_Fixed1 as "Fixed1"


  • 9.  RE: Adding a decimal to string values

    Employee
    Posted 03-03-2010 15:10

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rboccuzzi

    There are different ways you could do this. [EDIT: I worked on this while Ernest posted the above, so here is ANOTHER way ]

    I choose to break apart the list, to one element per row, and then reassemble it using an agg. First, I added a RowIndex, so I could reassemble using the Agg correctly, ensuring that only the ones that were separated got joined.

    I attached a graph that does this.

    Cheers
    Rich
    Attachments:
    example.brg


  • 10.  RE: Adding a decimal to string values

    Employee
    Posted 03-03-2010 15:40

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    I like how your version avoids the conversion from string to integer and back to string. This method probably runs faster.

    Originally posted by: rboccuzzi
    					

    There are different ways you could do this. [EDIT: I worked on this while Ernest posted the above, so here is ANOTHER way ]

    I choose to break apart the list, to one element per row, and then reassemble it using an agg. First, I added a RowIndex, so I could reassemble using the Agg correctly, ensuring that only the ones that were separated got joined.

    I attached a graph that does this.

    Cheers
    Rich


  • 11.  RE: Adding a decimal to string values

    Employee
    Posted 03-04-2010 15:01

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Iain Sanders

    This was really helpful!

    The example with the explanation given below worked a treat!

    Very straightforward, efficient and effective.

    Thank you so much!

    Iain

    Originally posted by: rboccuzzi
    					

    There are different ways you could do this. [EDIT: I worked on this while Ernest posted the above, so here is ANOTHER way ]

    I choose to break apart the list, to one element per row, and then reassemble it using an agg. First, I added a RowIndex, so I could reassemble using the Agg correctly, ensuring that only the ones that were separated got joined.

    I attached a graph that does this.

    Cheers
    Rich