1

This is my table: Table

I did this to populate it:

update AEROPORTO.RESERVA 
 set VALOR = random()*(5000-1500)+1500
where cod_reserva is not null

but random() has double precision and i what my values like this:

  • 111,11
  • 222,22

and round() doesn't use double precision.

I've tried to change the data type of the field Valor but didn't worked. Tried numeric, monetary and float.

I need to run a script that populates randomly this field with only 2 digits precision.

asked Jun 9, 2016 at 12:32
2
  • Don't use floating points to represent decimals, instead multiply your value by 100 to store an integer value and by 0.01 when reading. Commented Jun 9, 2016 at 12:35
  • If you only want two decimals, define your column with 2 decimals e.g.: numeric(12,2) Commented Jun 9, 2016 at 14:38

1 Answer 1

1

There are two round functions, round(dp or numeric) returns integer, and round(v numeric, s int) that returns numeric. Try the following:

update AEROPORTO.RESERVA 
 set VALOR = round((random()*(5000-1500)+1500)::numeric, 2)
where cod_reserva is not null
answered Jun 10, 2016 at 0:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.