Database automatically sets 1.00 to 1. How can I prevent that?

It took me a while to figure out that backendless was automatically converting my doubles to remove the decimal place. eg. 1.00000 gets changed to 1 or 9.00 to 9 etc. I have no idea why this is default behavior, but ideally I’d like to retain the zeros if they exist as the inputted data. Is this possible?

Thanks

Hello,
If the column’s type is DOUBLE it stores 1.00 but shows 1. And 1.0000000 is also 1. It’s not possible to retain zeros in the console.

Regards, Olga

Thanks. Its a real pain because my app uses the info to display financial figures eg £12.00. This will mean I’ll need to detect at the client side if the double has no decimal place to manually add two zeros.

It all seems a bit of a waste of time and I cant understand why Backendless removes them in the first place. Is there a reason? Thanks

Which SDK do you use and what does the query look like?

I’m using PHP and doing a PUT of the formatted double. But even when the formatted double is 1.000 it appears in backendless as 1.

curl_setopt_array($curl, array(

CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.backendless.com/'.$app_id.'/'.$secret_key.'/data/Game_Data/'.$game_object_id,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => json_encode(array(
current_pot => floatval(number_format($NEW_POT_AMOUNT,2))
)
),
CURLOPT_HTTPHEADER => array('Content-Type:application/json')
));

It doesn’t matter whether you send 1.0000 or 1 - the value saved in the database will be the same (which is 1). Such zero precision is used purely for display purposes. Thus, assuming your column has DOUBLE type, you may send integer numbers like 1 or 9 and they will still be doubles (same as 1.0, 1.00, 1.000 and so on).
You should use your number_format() function only when displaying such number, specifying the precision you need. So if you use number_format( X, 2 ) it will give you X.00; number_format( X, 4 ) => 1.0000. So it’s not something Backendless doesn’t do, it’s just how the double numbers work.

Here is a proper example of what you’re trying to accomplish.
Save:

curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.backendless.com/'.$app_id.'/'.$secret_key.'/data/Game_Data/'.$game_object_id,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => json_encode(array(
current_pot => floatval($NEW_POT_AMOUNT,2)
)
),
CURLOPT_HTTPHEADER => array('Content-Type:application/json')
));

Display:
echo '£’number_format( retrievedValue, 2)

And by the way, it seems PHP has a dedicated function for displaying money formats: http://php.net/manual/en/function.money-format.php

Okay Thanks. I’ve made the display adjustments at the client end. Cheers

i read in another post that it’s possible to control the format of a decimal with UI builder…
Anyone know how? I need my numbers to all display 4 digits past the decimal, at all times, even if it its .0000

I also don’t like how backendless rounds my decimals in the database, but it appears to be visual rounding and retaining the actual number. I really think that needs to be changed or made an option. I want to see my numbers darn it, but ultimately not that important if the value is correctly stored.

I am trying not to do this with code… I know I could figure out how to implement a java solution… eventually, or a php solution…

Hi Terrence,

What data type did you choose for that column in the database?

Regards,
Mark

Hi Mark,

I set the database columns to Decimal

Thanks

Hi Terrence,

I just tried and I see that for the whole numbers, the numbers to the right of the decimal point indeed do not show up.

Could you please provide an example of the rounding issue you mentioned?

Regarding the 4 digits past the decimal, perhaps there is a way to do it with codeless, but here’s a simple solution with JS:

Here’s my table that has a decimal number:

Here’s the data I have:

Head over to the Business Logic section and switch to Event Handlers. Click New Event Handler and configure the popup as shown below:

Click SAVE to create the handler, then click the handler in the list:

Add the following code in the code area:

res.result.forEach((x, i) =>  x.worth = (Math.round(x.worth * 100) / 100).toFixed(4));

make sure to replace worth with the name of your decimal column

Click the button to deploy your event handler:

Now if you return to the Data section, select your table and switch to REST Console, click GET there, you will see the result:

There will be the same effect when the requests are made from UI Builder. The event handler you added applies to all data retrieval requests for the specific table.

Hope this helps.

Regards,
Mark

Thank you Mark.
I’ll give that a go tomorrow.
It seems like a great solution!

Thanks for being helpful :wink:

I was able to implement the above for 14 different columns, and the correct results do show in the Rest Console.

The output through the ui builder is still not showing correctly.
I will screw around with it more this afternoon. I also do some math on the same page and I need the format for that result to also be 4 digits past the decimal so I have to also figure out how to format that number, which isn’t taken from the db.

could you please share a link to the page where we can see the problem?

https://stoutcolor.backendless.app/api/files/ui-builder/containers/default/index.html?page=inventoryPage

thanks,

I just checked the page and I do not see any values with 4 digits, are you sure your request is correct?

Hi,

The trouble is that for now I entered the values in through the console, and the console only shows two digits past the decimal so at the time, I think I just entered two digits.

None the less, I want the displayed info on the page to have four digits past.

When I run the the event handler that Mark told me to use, I get this in the rest console

	"stoneStock": 1000,
	"oilStock": 500,
	"foodRate": "1.0000",
	"stoneBaseRate": 2.25,
	"lapsidiaRate": "17.2500",
	"oreBaseRate": 2.75,
	"adaOreRate": "4.7500",
	"adamentiumRate": "15.2500",
	"metalBaseRate": 6,
	"oilRate": "21.7500",
	"ownerId": null,
	"foodBaseRate": 1,
	"lapsidiaStock": 500,
	"woodBaseRate": 1.5,
	"lapsidiaBaseRate": 17.25,
	"metalStock": 500,
	"cardanikaRate": "9.5000",
	"___class": "market",
	"plasticRate": "3.2500",
	"researchStock": 1000,
	"adaOreBaseRate": 4.75,
	"woodStock": 1000,
	"spiceRate": "32.5000",
	"veniditeBaseRate": 27.75,
	"spiceStock": 100,
	"oreStock": 1000,
	"objectId": "4E54ADCC-CD87-4F8A-A061-83173703D5BB",
	"cardanikaBaseRate": 9.5,
	"stoneRate": "2.2500",
	"plasticStock": 1000,
	"oilBaseRate": 21.75,
	"created": 1637439100504,
	"adamentiumStock": 500,
	"researchRate": "47.2500",
	"plasticBaseRate": 3.25,
	"veniditeRate": "27.7500",
	"adamentiumBaseRate": "15.25",
	"metalRate": "6.0000",
	"cardanikaStock": 500,
	"oreRate": "2.7500",
	"adaOreStock": 500,
	"spiceBaseRate": 32.5,
	"woodRate": "1.5000",
	"veniditeStock": 500,
	"updated": 1637439670545,
	"researchBaseRate": 47.25,
	"foodStock": 1000

The number I had it effect is the foodRate, woodRate, etc…
and as you can see it is giving me the value I want.

I have the value of the text field linked to foodRate, but instead of the showing the value 1.0000 it displays 1

Seems like I need to format the number in the content logic using js num.toFixed(4)
not sure what that should look like though.

As you can your response is different than on your UI page, so make sure you run a correct request.

Seems like I need to format the number in the content logic using js num.toFixed(4)
not sure what that should look like though.

yes, this is the right way
you can use Custom Code to use JS

try it out on my demo page

https://backendlessappcontent.com/7E25B48A-05FA-0B5D-FFAE-63CC0AC26C00/A09B7B01-A9D5-43E6-AF33-6A774E4BB455/files/ui-builder/containers/default/index.html?page=with4Digits

That did it Vladimir.

Thank you both for your help with this.