@@ -11,7 +11,7 @@ def format_currency_value(value):
11
11
value: The value to format (string or numeric)
12
12
13
13
Returns:
14
- str: Formatted value according to system number format
14
+ str: Formatted value according to system number format and currency precision
15
15
"""
16
16
# First ensure we have a float value to work with
17
17
if isinstance (value , str ):
@@ -20,19 +20,33 @@ def format_currency_value(value):
20
20
else :
21
21
value_float = float (value )
22
22
23
+ # Get currency precision from Frappe
24
+ try :
25
+ currency_precision = frappe .get_precision ("Currency" , "amount" )
26
+ if currency_precision is None :
27
+ currency_precision = 2 # Default to 2 decimal places
28
+ except :
29
+ currency_precision = 2 # Default to 2 decimal places if anything goes wrong
30
+
31
+ # Round the value to the specified precision
32
+ value_float = round (value_float , currency_precision )
33
+
34
+ # Format with the exact number of decimal places
35
+ value_str = f"{{:.{ currency_precision } f}}" .format (value_float )
36
+
23
37
# Get the number format from system settings
24
38
number_format = frappe .get_system_settings ('number_format' )
25
39
26
40
# Format according to the system's number format
27
41
if number_format == "#.###,##" : # European format (1.234,56)
28
- formatted_value = str ( value_float ) .replace ("." , "," )
42
+ formatted_value = value_str .replace ("." , "," )
29
43
elif number_format == "# ###.##" : # Format with space (1 234.56)
30
- integer_part , decimal_part = str ( value_float ) .split ("." )
31
- formatted_value = " " . join ([ integer_part , decimal_part ])
44
+ integer_part , decimal_part = value_str .split ("." )
45
+ formatted_value = f" { integer_part } { decimal_part } "
32
46
elif number_format == "#,###.##" : # US format (1,234.56)
33
- formatted_value = str ( value_float )
47
+ formatted_value = value_str
34
48
else :
35
49
# Default format if none of the above
36
- formatted_value = str ( value_float )
50
+ formatted_value = value_str
37
51
38
52
return formatted_value
0 commit comments