What is the best way to incorporate a BigDecimal from Scala.js into my JavaScript project?

Presented below is an example of a Scala object:

@JSExportTopLevel("Calculator")
object Calculator {

  @JSExport
  def calculate(): BigDecimal = 3.14
}

The exported singleton method can be invoked from a JavaScript application, but the result may not behave as expected:

let result = Calculator.calculate()
assert(result == 123); // passes
assert(result === 123); // fails
assert(result + 1 == 124); // fails, actually returns "1241"

Upon inspecting result in the browser console, it appears as follows:

Object { s_math_BigDecimal__f_bigDecimal: {…}, s_math_BigDecimal__f_mc: {…}, s_math_BigDecimal__f_computedHashCode: 1565550863 }
    s_math_BigDecimal__f_bigDecimal: Object { Ljava_math_BigDecimal__f__hashCode: 0, "Ljava_math_BigDecimal__f_java$math$BigDecimal$$_bitLength": 9, "Ljava_math_BigDecimal__f_java$math$BigDecimal$$_scale": 2, … }
    s_math_BigDecimal__f_computedHashCode: 1565550863
    s_math_BigDecimal__f_mc: Object { Ljava_math_MathContext__f_precision: 34, Ljava_math_MathContext__f_roundingMode: {…} }
    <prototype>: Object { constructor: $c_s_math_BigDecimal(bigDecimal, mc), hashCode__I: hashCode__I(), equals__O__Z: equals__O__Z(that), … }

        ...

        "$plus__s_math_BigDecimal__s_math_BigDecimal": function $plus__s_math_BigDecimal__s_math_BigDecimal(that)
        byteValue__B: function byteValue__B()

        ...
    
        toString__T: function toString__T()
        <prototype>: Object { constructor: $c_s_math_ScalaNumber() }

Understanding how to effectively utilize this outcome can be challenging. Is there a need to export a different type or pursue a different approach?

Answer №1

Manipulating a BigDecimal directly from JavaScript code is not possible because it does not export anything.

If you do not necessarily require big decimal semantics, consider returning a Double instead, which is a JavaScript number. However, if you do need to work with instances of BigDecimal, you will need to create another exported object with methods specifically designed for manipulating them.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to navigate using single-page navigation due to JavaScript malfunction

I'm struggling with this particular code and I'm hoping for some assistance. My experience with javascript is limited, so I'm reaching out for help instead. I am trying to implement the jquery single.page.nav feature that enables navigation ...

Handling TextChanged Event of a TextBox in ASP.NET using C#

I'm currently designing a POS screen that allows barcode scanning directly into a textbox. I want to implement a code behind procedure that adds the barcode-related data to the grid as soon as the textbox text changes. This is how my textbox looks: &l ...

What is causing the net::ERR_CONNECTION_RESET in Node JS and ExpressJS?

Our application, built on ExpressJS and NodeJS, is hosted on a Linode server and served at port 3000. Although the app has been functioning well for a year, we have recently encountered consistent connection errors. The errors vary, but they are mostly re ...

Converting Database Information to JSON Format for Mobile Authentication Form

Currently, I am working on a Mobile App project using Phonegap that requires users to log in before retrieving JSON data. This PHP page is responsible for connecting to the mobile site and fetching the necessary information. <?php $con = mysqli_connec ...

Validate if cookie has been established in Javascript

Hello everyone! I am trying to redirect a user to another page when a cookie is set by clicking a button. <a href="" onClick="SetCookie('pecCookie','this is a cookie','-1')"><button type="button" name="accept" clas ...

Learn the steps for showcasing user information in a tabular layout by utilizing session management with node.js, react.js, and SQL

This view page redirects users to their details Hello everyone, I am currently working on a project that includes two forms: login and user details. When a user logs in and fills out the details form, they should be redirected to a view page where they ca ...

Fill a form with jQuery and ajax data after it has been submitted

I'm working on a simple HTML form and I want to use Ajax to perform a lookup using a PHP file after entering data into the first field. The goal is to fetch information from an external source for the two remaining fields. <form method="post" acti ...

Finding the largest number that is less than a specified variable within an array

I have a JavaScript array and a variable, set up like this; var values = [0, 1200, 3260, 9430, 13220], targetValue = 4500; How can I efficiently find the largest value in the array that is less than or equal to the given variable? In the provided ex ...

Delve deeper or skim the surface, caught within the confines of angular

Is there a way to prevent the class for each notification from changing when I click on something? I want to create a read more/read less functionality in Angular for notifications, and have all notifications expand when the button is clicked. <div clas ...

Why would one utilize window.location?.search?.split?

Could someone explain the purpose of using window.location?.search?.split('=')[1] and why the value of id is set to window.location?.search?.split('=')[1]? Code: function EndScreen() { const [score, setScore] = React.useContext(Score ...

Can you explain the process of utilizing Angular databinding to display nested information?

I'm facing a challenge with databinding when working with nested arrays in multiple timeslots and windows. Despite understanding the basics, I can't seem to make it work no matter how I try different approaches. It's really frustrating not k ...

Scrapy-splash seems to be encountering difficulties in extracting elements during the scraping process

My attempt to extract data from this Why is Splash not working here? Does anyone have a solution for me? Your assistance will be highly appreciated! ...

Exploring the ins and outs of building JavaScript objects using constructors

When creating a new object with var obj = new Object(value); and assigning property values with obj.value = newValue, I encountered an issue where only one of these actions would work at a time. Is there a way to make both work within the same object decla ...

Reactstrap and React-router v4 are failing to redirect when there is a partial change in the address link

Within the header of my website, <NavItem> <NavLink tag={Link} to="/template/editor">Create New Template</NavLink> </NavItem> On the routing page of my website, <BrowserRouter> <div className="container-fluid"> ...

bulk purchase discount with HTML/JavaScript/PHP

I am looking to add a slider feature to my "Prices" page in order to provide customers with an instant quote based on the quantity they select. The slider should range from 1 to 500 or even up to 1000, but having an input box for customers to enter the qu ...

Tips for updating an input field using JavaScript

Here is a simple code snippet that I have written. <script language='javascript"> function check() {} </script> <div id="a">input type="text" name="b"> <input type="button" onClic ...

sequentially animating elements using animate css in a choreographed manner

I have created a unique jsfiddle with 20 boxes that I am trying to animate using the Animate.css plugin. The plugin can be found at daneden.me/animate. My goal is to animate each box one after the other in a sequential manner, but I seem to be having trou ...

Creating a stunning 2D image from a 3D scene through the power of raytracing with webgl and three.js

My goal is to project a 3D scene onto a 2D plane using ray tracing. Although my ultimate aim is volume rendering, I am currently struggling with the basics. I have set up a three.js scene with the viewing plane attached to the camera in front of it. The S ...

When PHP echo of json_encode triggers an error, AJAX status 200 will be raised

Despite everything running smoothly in the program below, an AJAX error is triggered: javascript: var data = { email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="026f6742656f636b6e2c616d6f">[email protect ...

Emulate a mouse click on an Angular element

I am facing a challenge where I need to execute Angular controller functions from outside of the application. Due to simplicity reasons, I am unable to make modifications directly to the Angular app itself. Ideally, the code should be able to run in the br ...