Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

How to keep the number with two decimal places?

How to keep the number with two decimal places?

g2m.agent
Collaborator Collaborator
2,579 Views
21 Replies
Message 1 of 22

How to keep the number with two decimal places?

g2m.agent
Collaborator
Collaborator

I have a float number that needs to be converted into a String.
Such as 43.30000024
I want it to keep two decimal places: 43.30
I found that the built-in nodes round_to_nearest, round_to_floor, round_to_ceiling cannot retain decimals.

so what is the solution?

 

maya_5kRckl6XT2.png

0 Likes
Accepted solutions (1)
2,580 Views
21 Replies
Replies (21)
Message 2 of 22

mjcg91
Collaborator
Collaborator
Accepted solution

Here's how I would do it using string. To increase the number of decimal, you can just increase the "size" on the sequence node.

clamp_decimal_with_string.jpg

Maxime Jeanmougin - Technical Artist
https://maximejeanmougin.com

Join the Bifrost Addicts community on Discord:
https://discord.gg/bifrost-addicts
Message 3 of 22

mcw0
Advisor
Advisor

Old fashion solution is to multiply by 100.  Convert to int.  And multiply by 0.01.

Message 4 of 22

mjcg91
Collaborator
Collaborator

@mcw0 wrote:

Old fashion solution is to multiply by 100.  Convert to int.  And multiply by 0.01.


That's actually the most efficient solution.

Maxime Jeanmougin - Technical Artist
https://maximejeanmougin.com

Join the Bifrost Addicts community on Discord:
https://discord.gg/bifrost-addicts
Message 5 of 22

g2m.agent
Collaborator
Collaborator

It doesn't seem to work. the problem seems to "number_to_string".

maya_zZNx1VAEW7.png

Message 6 of 22

mcw0
Advisor
Advisor

Why are you using a "number_to_string" after multiplying by 100?  I'm not in Maya right now so I can't verify this but can't you use a value node and set it to integer and connect the output of multiplying by 100 to that to convert to an integer?

 

Oh, I see what you are saying.  "number_to_string2" is where the issue comes from.  Yes, that's common when dealing with floats.  hhhmmm...

 

There are no truncate functions?

0 Likes
Message 7 of 22

g2m.agent
Collaborator
Collaborator

another example: "number_to_string" turns float "1.61" into string "1.61000001".

maya_brfrost graph_number_to_string_problem.png

Message 8 of 22

mcw0
Advisor
Advisor

Unfortunately, that's how floats behave.  0.0 != 0

 

 

0 Likes
Message 9 of 22

mcw0
Advisor
Advisor

Again, I'm not in Maya so just thinking out loud.  There must be a modulus function in Bifrost.  After you convert to integer, take the modulus of the integer to 100 (integer % 100).  That result is your decimal.  Now subtract that from your integer to get your whole number.  Now use "build_string" and use the whole number, "." and the modulus result!

Message 10 of 22

g2m.agent
Collaborator
Collaborator

Basically, I want to rebuild the After Effects Text layer in Maya. plug in any String or Number to display it on the screen (in the viewport).

I use the methods provided by Bruce.Lee and mspeer to input the String into the Python Expression slot of Type. The Python Expression slot only accepts Strings. The original post is here: https://forums.autodesk.com/t5/bifrost-forum/how-to-show-mesh-infomation-into-quot-type-quot-or-quot...

0 Likes
Message 11 of 22

g2m.agent
Collaborator
Collaborator

So mjcg91's method may be the only way.

Message 12 of 22

mcw0
Advisor
Advisor

I'm not sure what the AE text layer is.  But if you want to display any string or number in the viewport, you don't need Bifrost to do that.  I used a simple Python script.  I'm not on my work computer but I can post it tomorrow.

 

But just curious, did the modulus approach not work?

0 Likes
Message 13 of 22

g2m.agent
Collaborator
Collaborator

this is my connection:

maya_JJzjYoMRAr.png

 

When the number is less than -1 and greater than 0.01, the result is correct.

when the number is -0.017, the result is 0.1

when the number is 0.057, the result is 0.5

 

“Integer part” cannot recognize +0 and -0

"decimal part" cannot recognize 057 and 57

0 Likes
Message 14 of 22

mcw0
Advisor
Advisor

The +/- issue can be handled with a conditional.  But the "number_to_string" removing the first zeros is unavoidable but can also be handled with conditionals.  😞  I thought for sure we had it solved.  😞 😞

 

A shot mask like the one the plugin builds could be scripted with MEL.  I have scripts that could be "Frankensteined" into what you need (camera frustum and text editing).

 

But maybe the creator of the plugin could add that feature.  It wouldn't be difficult to do.

Message 15 of 22

mcw0
Advisor
Advisor

DOH!!!  As I typed my last reply, the solution hit me.  It will take a few more nodes but it's definitely doable.  Plug your input value into an "if" node.  If value is less than zero, add a "-" sign at the start of the "build_string" node.  And then for your decimal portion, another "if" node.  If less than 10, add a "0" after the "." in the "build_string" node.  By the way, the "0" should not be a "number_to_string" as that will probably not recognize the zero.  Just set it in the "build_string" node.

0 Likes
Message 16 of 22

g2m.agent
Collaborator
Collaborator

I think this graph is more complicated than mjcg91's graph and not easy to read.

Message 17 of 22

g2m.agent
Collaborator
Collaborator

I found a bug. when the input is an integer, it will produce the wrong results. such as

1 => 1.1

2 => 2.2

......
the new graph can solve this issue.

the only disadvantage is that it cannot round decimals.

number_to_string_v3number_to_string_v3

 

 

0 Likes
Message 18 of 22

mcw0
Advisor
Advisor

Capture13.PNG

 

This appears to work and handles negative inputs.  "split_string" uses "." as the separator.

0 Likes
Message 19 of 22

mcw0
Advisor
Advisor

OOPS!  This works!

 

Capture14.PNG

0 Likes
Message 20 of 22

g2m.agent
Collaborator
Collaborator

Unfortunately, it still doesn't work.

You can build this rig to test your graph.

1. create a cube, sphere, anything

2. select it, Create->Annotation...

3. in Node Editor, connecting like this:

maya_HzSnuBTKmR.png

 

0 Likes