1. Tomas Beran
  2. PowerBuilder
  3. Tuesday, 20 December 2022 15:44 PM UTC

Hi

Is there a direct way how to convert a decimal to a culture invariant string?

Standard method string( decimal ) returns the string converted according to the local user's setting. Like in Europe string(1.2) returns "1,2". Or "1'2" etc. What I want is "1.2".

I know there's many ways how to do it. I can look for the local separator in the string and change it. Or split the number into two parts and concatenate the string.

 

Benjamin Gaesslein Accepted Answer Pending Moderation
  1. Monday, 9 January 2023 09:08 AM UTC
  2. PowerBuilder
  3. # 1

This should work for all decimal numbers in PB.

decimal dec_num
string invariant_string

string first_part
string second_part

dec_num= 1.23456

first_part = string( truncate( dec_num, 0 ) )
second_part = mid ( string( abs( dec_num - truncate( dec_num, 0 ) ), '0.############################' ), 3 )

invariant_string =  first_part
if len(second_part) > 0 then
	invariant_string = invariant_string + '.' + second_part
end if
//if you want to display "x.0" if the decimal part is zero, replace the mask for second_part with
//0.0########################### and just do this instead:

// invariant_string = first_part + '.' + second_part

Edit: added an abs() for the decimal part to make it work for negative numbers

Comment
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Friday, 23 December 2022 16:23 PM UTC
  2. PowerBuilder
  3. # 2
Decimal ldec_decimal
Long ll_number
String ls_number, ls_decimal, ls_whole, ls_display

ldec_decimal = 1234.56

ll_number = ldec_decimal * 100
ls_number = String(ll_number)
ls_decimal = Right(ls_number, 2)
ls_whole = Left(ls_number, Len(ls_number) - 2)
ls_display = ls_whole + "." + ls_decimal

MessageBox("Decimal", ls_display)
Comment
  1. Tomas Beran
  2. Tuesday, 27 December 2022 10:03 AM UTC
Hi

Now imagine you have 15 decimal points. Imagine there are countries that use '()' around negative numbers. This code is dangerous.
  1. Helpful
  1. Roland Smith
  2. Tuesday, 3 January 2023 13:21 PM UTC
I should note that I just typed this code from the top of my head, I have not run it so don't know if it works.
  1. Helpful
There are no comments made yet.
Tomas Beran Accepted Answer Pending Moderation
  1. Wednesday, 21 December 2022 16:32 PM UTC
  2. PowerBuilder
  3. # 3

Hi David

If your suggestion worked it would be an error. Formats format to regional strings and I think this is correct.

I tried messagebox("test",string(3.14, '0.00')) and the result was:

---------------------------
test
---------------------------
3,14
---------------------------
OK   
---------------------------

 

I know there is a few special parameters to string() like "address" so I'm wondering if there's no string ( 3.14 , "invariant" ) alternative.

Comment
  1. Tomas Beran
  2. Thursday, 22 December 2022 11:36 AM UTC
I'm pretty sure

string(long( ld_val )) + "." + string(ld_val - long( ld_val )) // know it's buggy, take it just as an idea

is faster than a database operation.
  1. Helpful
  1. Sivaprakash BKR
  2. Friday, 23 December 2022 12:15 PM UTC
Can you try

string(abs(ld_val), '###,###,##0.00')
  1. Helpful
  1. Tomas Beran
  2. Tuesday, 27 December 2022 10:06 AM UTC
No, formatting based solutions don't work.



---------------------------

test

---------------------------

3,14

---------------------------

OK

---------------------------

  1. Helpful
There are no comments made yet.
David Peace (Powersoft) Accepted Answer Pending Moderation
  1. Wednesday, 21 December 2022 16:15 PM UTC
  2. PowerBuilder
  3. # 4

Hi Tomas

you could try string(ld_decimal, '0.00')

regards

David

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.