help with javascript

91 views
Skip to first unread message

grahamsc...@gmail.com

unread,
Jan 24, 2023, 7:20:56 PM1/24/23
to mementodatabase
I have this javascript that is working perfect to add a prefix in front of currency and also give the thousand separator.
The field it pulls the value from is called accomodation total but if the field has no data in it I get a message to say the
cannot call method "to String" of null
However if I go and put a 0 in the accommodation field by default I dont see the message.
Its no big issue, just wondering if there is anything I can add into my code so it wont display the message if accommodation total is blank.
here is my code

function numberWithCommas(x)
{
var parts = x.toString().split(".");
parts[0] =
parts[0].replace
(
/\B(?=(\d{3})+(?!\d))/g, ","
);
return parts.join(".");
}
var number = field('accomodation total');
var formatedNumber;
formatedNumber = numberWithCommas(number);
formatedNumber = number/=(‘total travellers’);
formatedNumber = number.toFixed(2);
formatedNumber = "$ " + formatedNumber;
result = formatedNumber;

Er Mo

unread,
Jan 25, 2023, 1:28:37 PM1/25/23
to mementodatabase
Hallo
Was ist der Feldtyp von den du den Wert beziechst . Es müsste ein Zahlenwert sein und kein Text . Dein Skript ist ein Aufruf einer Funktion , ich würde mit einen if() brüfen ob der Wert nicht " null " ist . if( "Wert von Feld "!=null) und dann die Funktion aufrufen .

Hi
What is the field type you are getting the value from. It would have to be a numeric value and not text. Your script is a call to a function, I would use an if() to check if the value isn't "null". if( "Value of field "!=null) and then call the function.

Ernst

Bill Crews

unread,
Jan 25, 2023, 6:03:52 PM1/25/23
to grahamsc...@gmail.com, mementodatabase
Add the booms green line to your script...

var number = field('accomodation total');
number = (number == null) ? 0 : number;

var formatedNumber;
formatedNumber = numberWithCommas(number);
formatedNumber = number/=(‘total travellers’);
formatedNumber = number.toFixed(2);
formatedNumber = "$ " + formatedNumber;
result = formatedNumber;
--
You received this message because you are subscribed to the Google Groups "mementodatabase" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mementodataba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mementodatabase/88e0e4a2-2b7e-4c26-be8e-092ff38eae5dn%40googlegroups.com.

Graham Wood

unread,
Jan 25, 2023, 7:21:10 PM1/25/23
to mementodatabase
Thankyou guys much appreciated once again this has fixed my issue
Reply all
Reply to author
Forward
0 new messages