Highlighted
Absent Member.
Absent Member.
2488 views

TextBox with currency format

Jump to solution

Hi, I whant to know, how I can make the currency format to textbox, like of Cobol

77  Invoice-Amount      Pic $$,$$9.99.

I tryed other why, but is so strange and not so functional..., it's possible Automated teller machine ???

for example: the user will be type this number 5124.25

the user types 5: 0,05

the user types 1: 0,51

the user types 2: 5,12

the user types 4: 51,24

the user types 2: 512,42

the user types 5: 5124,25

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: TextBox with currency format

Jump to solution

I believe that I got this to work correctly by using the following example which uses the TextChanged event of the textbox.

 

       method-id textBox1_TextChanged final private.
       procedure division using by value sender as object e as type System.EventArgs.
        *>Remove previous formatting, or the decimal check will fail including leading zeros
           declare myvalue as string = 
           textBox1::Text::Replace(",", "")::Replace("$", "")::Replace(".", "")::TrimStart('0')
           declare ul as decimal
        *>Check we are indeed handling a number
           if type Decimal::TryParse(myvalue, ul)
              set ul to ul / 100
        *>Unsub the event so we don't enter a loop
              invoke textBox1::remove_TextChanged(new System.EventHandler(self::textBox1_TextChanged))
        *>Format the text as currency
              set textBox1::Text to type 
              String::Format(type System.Globalization.CultureInfo::CreateSpecificCulture("en-US"), "{0:C2}", ul)
              invoke textBox1::add_TextChanged(new System.EventHandler(self::textBox1_TextChanged))
              invoke textBox1::Select(textBox1::Text::Length, 0)
           end-if
           declare goodToGo as condition-value = self::TextisValid(textBox1::Text)
           if not goodToGo
              set textBox1::Text to "$0.00"
              invoke textBox1::Select(textBox1::Text::Length, 0)
           end-if
           goback.
    
       end method.
       method-id TextisValid private.
       procedure division using by value mytext as string
                          returning ret-bool as condition-value.
           declare money as type System.Text.RegularExpressions.Regex = 
              new System.Text.RegularExpressions.Regex("^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$")
           set ret-bool to money::IsMatch(mytext)
           goback.
       end method.
       

View solution in original post

0 Likes
3 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: TextBox with currency format

Jump to solution

I believe that I got this to work correctly by using the following example which uses the TextChanged event of the textbox.

 

       method-id textBox1_TextChanged final private.
       procedure division using by value sender as object e as type System.EventArgs.
        *>Remove previous formatting, or the decimal check will fail including leading zeros
           declare myvalue as string = 
           textBox1::Text::Replace(",", "")::Replace("$", "")::Replace(".", "")::TrimStart('0')
           declare ul as decimal
        *>Check we are indeed handling a number
           if type Decimal::TryParse(myvalue, ul)
              set ul to ul / 100
        *>Unsub the event so we don't enter a loop
              invoke textBox1::remove_TextChanged(new System.EventHandler(self::textBox1_TextChanged))
        *>Format the text as currency
              set textBox1::Text to type 
              String::Format(type System.Globalization.CultureInfo::CreateSpecificCulture("en-US"), "{0:C2}", ul)
              invoke textBox1::add_TextChanged(new System.EventHandler(self::textBox1_TextChanged))
              invoke textBox1::Select(textBox1::Text::Length, 0)
           end-if
           declare goodToGo as condition-value = self::TextisValid(textBox1::Text)
           if not goodToGo
              set textBox1::Text to "$0.00"
              invoke textBox1::Select(textBox1::Text::Length, 0)
           end-if
           goback.
    
       end method.
       method-id TextisValid private.
       procedure division using by value mytext as string
                          returning ret-bool as condition-value.
           declare money as type System.Text.RegularExpressions.Regex = 
              new System.Text.RegularExpressions.Regex("^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$")
           set ret-bool to money::IsMatch(mytext)
           goback.
       end method.
       

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: TextBox with currency format

Jump to solution

It's work perfectly!!!!!!!!!!!!!!!!

But, how to make a regex for a brazilian currency ??
We use, for example: R$85.454,57
The decimal point is comma and we use the letter R before $

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: TextBox with currency format

Jump to solution

Ok, I solved, I but my regex code for this:

                   ^R\$ ?([1-9]{1}[\d]{0,2}(.[\d]{3})*(,[\d]{0,2})?|[1-9]{1}Drinks{0,}(,[\d]{0,2})?|0(,[\d]{0,2})?|(,[\d]{1,2})?)$

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.