# Need Math Help with Thermometer

Please take a moment to consider if this thread is worth bumping.

## Recommended Posts

I have a thermometer object that works fine, but I'd like a math geek to help me with the result that gets posted to chat, namely, the amount of numbers reported after the decimal.

Here's the code snippet:

float temperatureF = ((((pascal * (2 * llPow(10,22)))/(1.8311*llPow(10,20))/ 8.3)/19.85553747) + (sun.z * 10));

Which returns a value like this:

73.688260 °F  ( 23.160140 °C)

There are too many numbers here. I'd prefer just one or two after the decimal,  i.e. 73.6 °F ( 23.1 °C).
How do I re-write the snippet above to delete the last five numbers after the decimal?

##### Share on other sites

Posted (edited)

There are various methods, but off the top of my head:

```float fTemp = formula; // calculate the temperature with full accuracy.
fTemp = llRound(fTemp*10)*0.1; // round to desired accuracy.
string sTemp = (string)fTemp; // convert to string;
sTemp = llGetSubString(sTemp,0,llSubStringIndex(sTemp,".")+1); // remove trailing 0's.```

(untested)

llInsertString() the decimal point into a llRound()ed integer might be a viable alternative approach, but llInsertString doesn't take negative indexes.

Edited by Quistess Alpha
• 1
##### Share on other sites

Hi Caldora,

Pass temperatureF variable to that function, with a precision value of 1. The result will be a string version of the value with one digit after the decimal point.

I haven't tested those functions, so my precision value might be off by one.

• 2
##### Share on other sites

Posted (edited)
29 minutes ago, Madelaine McMasters said:

A nice example, but as I read it, it returns a fixed number of digits rather than a fixed precision:

Fixed to 3 Digits Fixed Precision 10ths place
12.7 12.7
101. 101.5
1.23 1.2

Whether there's a difference depends on the range of the input though.

ETA: Actually I'm wrong as I read through it too quickly and didn't think through the weirdness of SL's default presentation, and tacit negative indexes.

Edited by Quistess Alpha
• 1
• 1
##### Share on other sites

Posted (edited)

Thanks everyone!! The llRound  function did the trick.

" +(string)llRound(temperatureF) + " °F  ( "+(string)llRound((temperatureF - 32) * 5/9) +" °C)";

gives me  74 °F  ( 23 °C) currently, which looks a lot cleaner in chat.

Edited by Caldora Beaumont
• 2
##### Share on other sites

6 minutes ago, Caldora Beaumont said:

llRound((temperatureF - 32) * 5/9)

I think order of operations works in your favor there, but of it were me, I'd use 5.0/9.0 to avoid mixing integer and float arithmetic, or just multiply by .5555 .

##### Share on other sites

I may have missed something, but if precision is the issue, why not use integers and start off multiplying everything by, say, 10000? Then, using only integer math, the result should be pretty precise. Just with an implied final decimal place.

##### Share on other sites

1 minute ago, Quistess Alpha said:

I think order of operations works in your favor there, but of it were me, I'd use 5.0/9.0 to avoid mixing integer and float arithmetic, or just multiply by .5555 .

OK. I am neither much of a scripter or proficient in math, but I cribbed the snippets above from multiple sources out there and worked for days to get it to work the way I wanted to. That last bit was defying my admittedly derivative scripting efforts. I applied the llRound function in a few other places, too. Appreciate all the help!

In case anyone's interested, this is what displays in my chat window when the thermometer is touched:

..::: Isla De Castaña Weather:::.
Temperature:
74 °F (23 °C)
Barometric Pressure:
0.030128 ”/Hg (0.102024 kPa)
Wind Speed:
18 mph
8 m/S
15 kts
Wind Direction: 277°
(N=0°, E=90°, S=180°, W=270°).
Cloud coverage: 69%.
Chance of rain: 64%.
Have a nice day!  😃

##### Share on other sites

7 minutes ago, Love Zhaoying said:

I may have missed something, but if precision is the issue, why not use integers and start off multiplying everything by, say, 10000? Then, using only integer math, the result should be pretty precise. Just with an implied final decimal place.

Actually, precision is not especially important here. I can verify that the temp does indeed change when the sun position does, but as long as it's ballpark, I'm good ! 🙂

• 1
##### Share on other sites

If you just trim floating part of a number, you can use a snippet like this:

```float floatNumber = 74.89450;

default {
touch_start(integer total_number) {
list number = llParseString2List((string)floatNumber, ["."], []); // Extract "floatNumber" in 2 parts
string integerPart = llList2String(number, 0); // 74
string floatPart = llList2String(number, 1); // 89450
integer n = 2; // Number of decimals
llOwnerSay(integerPart + "." + llGetSubString(floatPart, 0, n - 1)); // Output: 74.89
}
}```